Skip to content

Instantly share code, notes, and snippets.

@johnnyreilly
Last active September 2, 2015 14:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johnnyreilly/22f7c05b02c2129b89ef to your computer and use it in GitHub Desktop.
Save johnnyreilly/22f7c05b02c2129b89ef to your computer and use it in GitHub Desktop.
Helper functions for sorting arrays by multiple criteria
function composeComparers(...comparers) {
return comparers.reduce((prev, curr) => (a, b) => prev(a, b) || curr(a, b));
}
function stringComparer(propLambda) {
return (obj1, obj2) => {
const obj1Val = propLambda(obj1) || '';
const obj2Val = propLambda(obj2) || '';
return obj1Val.localeCompare(obj2Val);
};
}
function numberComparer(propLambda) {
return (obj1, obj2) => {
const obj1Val = propLambda(obj1);
const obj2Val = propLambda(obj2);
if (obj1Val > obj2Val) {
return 1;
}
else if (obj1Val < obj2Val) {
return -1;
}
return 0;
};
}
function reverse(comparer) {
return (obj1, obj2) => comparer(obj2, obj1);
}
/* - Example usage
const foodInTheHouse = [
{ what: 'cake', daysSincePurchase: 2 },
{ what: 'apple', daysSincePurchase: 8 },
{ what: 'orange', daysSincePurchase: 6 },
{ what: 'apple', daysSincePurchase: 2 },
];
const foodInTheHouseSorted = foodInTheHouse.sort(composeComparers(
stringComparer(x => x.what),
reverse(numberComparer(x => x.daysSincePurchase))
));
console.log(foodInTheHouseSorted);
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment