Last active
August 18, 2016 03:30
-
-
Save chengjianhua/f042079c823e7ef8fafe2df29168d364 to your computer and use it in GitHub Desktop.
sort according to the specified field in object
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
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