Last active
June 20, 2018 10:16
-
-
Save Shwartz/93e78550af29e8b393fe246b65b0e67f to your computer and use it in GitHub Desktop.
ES6 style sorting helpers for objects
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
const asc = (left, right) => left < right ? -1 : left > right ? 1 : 0; | |
const desc = (left, right) => left > right ? -1 : left < right ? 1 : 0; | |
const sort = (array, column, order) => array.sort(({[column]: left}, {[column]: right}) => order(left, right)); | |
export const sortAsc = (array, column) => sort(array, column, asc); | |
export const sortDesc = (array, column) => sort(array, column, desc); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage
Let's say you want to sort out this obj by column B:
get new array sorted by column B Descending