Sort array of objects by reference array

I have two array of objects, they are essentially copies but the reference array uses a different order.

let result = {"articleData":[{"id":"1","identifier":"Article1"},{"id":"2","identifier":"Article2"},{"id":"3","identifier":"Article3"},{"id":"4","identifier":"Article4"},{"id":"5","identifier":"Article5"},{"id":"6","identifier":"Article6"}]};

let resultReference = {"articleData":[{"id":"3","identifier":"Article3"},{"id":"4","identifier":"Article4"},{"id":"1","identifier":"Article1"},{"id":"2","identifier":"Article2"},{"id":"6","identifier":"Article6"},{"id":"5","identifier":"Article5"}]};

What I want to do is: currently I get some data from my API but it is in the wrong order (how it currently is in "result" array). I want the data to be in the exact same order as the "resultReference" array but how can I achieve this? Is there a minimal approach?

https://jsfiddle.net/g1rmhq9f/

Thanks for any advice

2 answers

  • answered 2022-05-04 11:43 jsN00b

    Presented below is one possible way to achieve the desired objective.

    Code Snippet

    // custom sort method
    const mySort = (arr, refArr) => (
      // use structured-clone to deep-clone "arr"
      // to avoid mutating/modifying it
      structuredClone(arr).
      sort(
        (
          { id: aid }, { id: bid  }     // de-structure & rename "id" props
        ) => (                          // a's ref-index minus b's ref-index 
          refArr.findIndex(({ id }) => id === aid) -
          refArr.findIndex(({ id }) => id === bid)
        )
      )
    );
    
    let result = {"articleData":[{"id":"1","identifier":"Article1"},{"id":"2","identifier":"Article2"},{"id":"3","identifier":"Article3"},{"id":"4","identifier":"Article4"},{"id":"5","identifier":"Article5"},{"id":"6","identifier":"Article6"}]};
    
    let resultReference = {"articleData":[{"id":"3","identifier":"Article3"},{"id":"4","identifier":"Article4"},{"id":"1","identifier":"Article1"},{"id":"2","identifier":"Article2"},{"id":"6","identifier":"Article6"},{"id":"5","identifier":"Article5"}]};
    
    console.log(
      "sorted version of result object's articleData array: ",
      mySort(result?.articleData, resultReference?.articleData)
    );
    .as-console-wrapper { max-height: 100% !important; top: 0 }

    Explanation

    Inline comments added to the snippet above.

  • answered 2022-05-04 11:56 Martin Konecny

    Assuming "I want the data to be in the exact same order as the "resultReference" array" means you just want the ID to be in ascending order, here is a much simpler solution:

    let resultReference = {"articleData":[{"id":"3","identifier":"Article3"},{"id":"4","identifier":"Article4"},{"id":"1","identifier":"Article1"},{"id":"2","identifier":"Article2"},{"id":"6","identifier":"Article6"},{"id":"5","identifier":"Article5"}]};
    
    resultReference.articleData.sort(function(a, b) {
      let id1 = parseInt(a.id);
      let id2 = parseInt(b.id);
    
      return id1 - id2;
    })
    
    /*
    Output:
    {
      articleData: [
        { id: '1', identifier: 'Article1' },
        { id: '2', identifier: 'Article2' },
        { id: '3', identifier: 'Article3' },
        { id: '4', identifier: 'Article4' },
        { id: '5', identifier: 'Article5' },
        { id: '6', identifier: 'Article6' }
      ]
    }
    */
    
    

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum