Last active
December 30, 2020 00:13
-
-
Save LeandrodeLimaC/ab977aec992f2a2e43e592cea22de820 to your computer and use it in GitHub Desktop.
Sort list by parameters, you can specify a default field, the actual field to be filtered and a order for comparation
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 sortList = ( list, field_default, field = field_default, order = 'a_z' ) => { | |
list.sort(comparator(field, order)); | |
}; | |
const comparator = (field, order) => { | |
return (a, b) => { | |
const comparation_table = { | |
a_z: a[field] == b[field] ? 0 : a[field] < b[field] ? -1 : 1, | |
z_a: a[field] == b[field] ? 0 : a[field] > b[field] ? -1 : 1, | |
}; | |
return comparation_table[order]; | |
}; | |
}; | |
export default sortList; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment