-
-
Save onildoaguiar/637ad2b78f3a03e203bd193ee4d1a610 to your computer and use it in GitHub Desktop.
Example of functional sorting an object array by name and age
This file contains hidden or 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
const persons = [ | |
{ 'name':'Luke', 'age':10}, | |
{ 'name':'Leia', 'age':10}, | |
{ 'name':'Vader', 'age':40} | |
]; | |
const ascending = x => y => x > y; | |
const ageSort = (x, y) => ascending(x.age)(y.age); | |
const nameSort = (x, y) => ascending(x.name)(y.name); | |
const ascendingSort = persons.sort(nameSort).sort(ageSort); | |
console.log(ascendingSort); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Your example is not working, the ascending function is wrong. Should be: