Skip to content

Instantly share code, notes, and snippets.

@LeandrodeLimaC
Last active December 30, 2020 00:13
Show Gist options
  • Save LeandrodeLimaC/ab977aec992f2a2e43e592cea22de820 to your computer and use it in GitHub Desktop.
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
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