Last active
September 16, 2020 15:00
-
-
Save lutangar/3189d4f240b87d0ca9ccb1b3ad90691a to your computer and use it in GitHub Desktop.
Compare, sort, javascript
This file contains 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
export const stringCompare = (a, b) => a.toLocaleLowerCase().localeCompare(b.toLocaleLowerCase()) | |
export const numberCompare = (a, b) => parseFloat(a) - parseFloat(b) | |
export const dumbCompare = (a, b) => a - b | |
export const dateCompare = (a, b) => (a < b ? -1 : a > b ? 1 : 0) | |
export const sortByArchived = (a, b) => dumbCompare(a.archived, b.archived) | |
export const sortByDate = (a, b) => numberCompare(a.date, b.date) * -1 | |
export const sortInverter = sortFunction => (a, b) => sortFunction(a, b) * -1 | |
export const composeSort = (sortA, sortB) => (a, b) => sortA(a, b) || sortB(a, b) | |
const sortedNews = news.sort(composeSort(sortByArchived, sortByDate)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment