Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save helabenkhalfallah/9d31b146b5a0dd9646f41521cc4bb0ed to your computer and use it in GitHub Desktop.
Save helabenkhalfallah/9d31b146b5a0dd9646f41521cc4bb0ed to your computer and use it in GitHub Desktop.
JS Curried Function (universal sort by)
const universalSortBy = property => (a, b) => {
if (a[property] < b[property]) {
return -1;
}
if (a[property] > b[property]) {
return 1;
}
return 0;
};
const users = [];
const sortByName = universalSortBy(‘name');
const sortedByNameUsers = users.sort(sortByName);
const sortByNote = universalSortBy('note');
const sortedByNoteUsers = users.sort(sortByNote);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment