Skip to content

Instantly share code, notes, and snippets.

@lutangar
Last active September 16, 2020 15:00
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 lutangar/3189d4f240b87d0ca9ccb1b3ad90691a to your computer and use it in GitHub Desktop.
Save lutangar/3189d4f240b87d0ca9ccb1b3ad90691a to your computer and use it in GitHub Desktop.
Compare, sort, javascript
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