Skip to content

Instantly share code, notes, and snippets.

@jamesslomka
Last active March 25, 2022 18:40
Show Gist options
  • Save jamesslomka/2b67922607b2cfd7204c0df4b98b1c41 to your computer and use it in GitHub Desktop.
Save jamesslomka/2b67922607b2cfd7204c0df4b98b1c41 to your computer and use it in GitHub Desktop.
Sorting filter
// 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