-
-
Save beelarr/1719a026b56de899fa32ca52d7abff59 to your computer and use it in GitHub Desktop.
Sort an Array of Objects by a different sorting Array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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