Skip to content

Instantly share code, notes, and snippets.

@onildoaguiar
Forked from seckBalla/ascendingSort.js
Created November 2, 2018 14:33
Show Gist options
  • Save onildoaguiar/637ad2b78f3a03e203bd193ee4d1a610 to your computer and use it in GitHub Desktop.
Save onildoaguiar/637ad2b78f3a03e203bd193ee4d1a610 to your computer and use it in GitHub Desktop.
Example of functional sorting an object array by name and age
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);
@tomschall
Copy link

Your example is not working, the ascending function is wrong. Should be:

const ascending = (x) => (y) => (x > y) - (x < y);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment