Created
May 15, 2021 22:28
-
-
Save helabenkhalfallah/9d31b146b5a0dd9646f41521cc4bb0ed to your computer and use it in GitHub Desktop.
JS Curried Function (universal sort by)
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 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