0
const r = ['a', 'b', 'c', 'l', 'p'];

const arr = [{
        name: "ss3",
        id: 'c'
    }, {
        name: "ss2",
        id: 'b'
    }, {
        name: "ss4",
        id: 'p'
    }, {
        name: "ss1",
        id: 'a'
    }]

var newArray =arr.map((i)=>{
  let e = r[i];

  if(i.id===e){
    return i
  }
})

console.log(newArray)

Expected output

const arr = [{
        name: "ss1",
        id: 'a'
    }, {
        name: "ss2",
        id: 'b'
    }, {
        name: "ss3",
        id: 'c'
    }, {
        name: "ss4",
        id: 'p'
    }
]

Given two arrays r and arr, I wish to sort arr with respect to r, i.e. in alphabetical order by id.

https://jsbin.com/yitijiboso/edit?html,js,output

slevy1
  • 3,797
  • 2
  • 27
  • 33
user944513
  • 12,247
  • 49
  • 168
  • 318
  • 1
    Possible duplicate of [Sort array containing objects based on another array](https://stackoverflow.com/questions/13518343/sort-array-containing-objects-based-on-another-array) – str Oct 29 '18 at 16:17

3 Answers3

0

I think this might be a concise (although not very performant) way to achieve the desired output:

const arr1 = ['a', 'b', 'c', 'l', 'p'];
const arr2 = [
    {
        name: "ss3",
        id: 'c'
    },
    {
        name: "ss2",
        id: 'b'
    }, {
        name: "ss4",
        id: 'p'
    },
    {
        name: "ss1",
        id: 'a'
    }
];

arr2.sort((a, b) => arr1.indexOf(a.id) - arr1.indexOf(b.id));
console.log(arr2);
Petr Broz
  • 8,891
  • 2
  • 15
  • 24
0

Easy:

  1. make a map from main 'arr' keyBy 'id' https://www.npmjs.com/package/lodash.keyby
  2. loop across 'r', if key exist in new map, get value and push to new array

    const arrMap = _.keyBy(arr, 'id');
    let newR = [];
    r.forEach( key => {
       if ( arrMap[key] ) {
          newR.push( arrMap[key] );
       }
    } );
    console.log( 'new array', newR );
    
Dmitri Algazin
  • 3,332
  • 27
  • 30
0

Taking a clue from @Petr Broz, here's my suggestion:

const r = ['a', 'b', 'c', 'l', 'p'];
const arr = [
    {
        name: "ss3",
        id: 'c'
    },
    {
        name: "ss2",
        id: 'b'
    }, {
        name: "ss4",
        id: 'p'
    },
    {
        name: "ss1",
        id: 'a'
    }
];

arr.sort((a, b) => r.indexOf(a.id) > r.indexOf(b.id));
console.log(arr);

Main difference is that this code utilizes the arrays as named by the OP and uses a greater than comparison operator. However, if you just want to have the array arr sorted in alphabetical order you don't need to compare it with array r:

            const arr = [
                {
                    name: "ss3",
                    id: 'c'
                },
                {
                    name: "ss2",
                    id: 'b'
                }, {
                    name: "ss4",
                    id: 'p'
                },
                {
                    name: "ss1",
                    id: 'a'
                }
            ];

           arr.sort(function(a, b) 
           {
             
             if (a.id > b.id) {
                   return 1;
             }
             else
             if (a.id < b.id) {
                    return -1;
             
             }
             else
             {
                return 0;
             } 
           });

           console.log(arr);

Note, in this example the return values are numeric instead of boolean values which would be helpful if the array to be sorted were to have duplicate values.

slevy1
  • 3,797
  • 2
  • 27
  • 33
  • please do not use a boolean value as return value for sorting, because this approach does not return `-1` and sometimes needed. the sorting algo need longer than it has to be. – Nina Scholz Oct 29 '18 at 17:20