Skip to content

Instantly share code, notes, and snippets.

  • Save beelarr/1719a026b56de899fa32ca52d7abff59 to your computer and use it in GitHub Desktop.
Save beelarr/1719a026b56de899fa32ca52d7abff59 to your computer and use it in GitHub Desktop.
Sort an Array of Objects by a different sorting Array
// Sort an Array of Objects by a different sorting Array
const sortedList = ['alice', 'clara', 'bob'];
const people = [{
name: 'alice',
age: 101
}, {
name: 'bob',
age: -10
}, {
name: 'clara',
age: 314.159
}];
const result = people.map(person => {
const name = sortedList.indexOf(person.name);
return [name, person]
}).sort().map(personList => personList[1])
console.log(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment