Skip to content

Instantly share code, notes, and snippets.

@chengjianhua
Last active August 18, 2016 03:30
Show Gist options
  • Save chengjianhua/f042079c823e7ef8fafe2df29168d364 to your computer and use it in GitHub Desktop.
Save chengjianhua/f042079c823e7ef8fafe2df29168d364 to your computer and use it in GitHub Desktop.
sort according to the specified field in object
function _sort({array, sortField, order}) {
order = order.toLowerCase();
const isAsc = order === Order.ASC;
return array.sort((a, b) => {
let valueA = a[sortField] === null ? '' : a[sortField];
let valueB = b[sortField] === null ? '' : b[sortField];
if(!isAsc) {
const temp = valueA; valueA = valueB; valueB = temp;
}
return typeof valueA === 'string' ? valueA.localeCompare(valueB) :
valueA > valueB ? 1 : (valueA < valueB ? -1 : 0);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment