Last active
March 25, 2022 18:40
-
-
Save jamesslomka/2b67922607b2cfd7204c0df4b98b1c41 to your computer and use it in GitHub Desktop.
Sorting filter
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
// Inspired by https://www.freecodecamp.org/news/supercharged-sorts-in-javascript/ | |
const sortBy = (prop) => (a, b) => (a[prop] ? -1 : b[prop] ? 1 : 0); | |
const sortByPair = (prop1, prop2) => (a, b) => (a[prop1] && a[prop2] ? -1 : b[prop1] && b[prop2] ? 1 : 0); | |
export default function superChangedSort(addresses) { | |
const byDefaultPair = sortByPair('someField', 'someOtherField'); | |
const byDefault = sortBy('default'); | |
const byName = sortBy('name'); | |
return [...addresses].sort( | |
(a, b) => byDefaultPair(a, b) || byDefault(a, b) || byName(a, b), | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment