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

1

3 Answers 3

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);

Sign up to request clarification or add additional context in comments.

Comments

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 );
    

Comments

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.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.