Created
September 13, 2018 16:34
-
-
Save joewright/bbb40c9527f39900b41211303a7f1745 to your computer and use it in GitHub Desktop.
_.sortBy example
This file contains hidden or 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
var data = [{ | |
active: false, | |
lastName: 'johnson' | |
}, { | |
active: true, | |
lastName: 'wow' | |
}, { | |
active: false, | |
lastName: 'awesome' | |
}, { | |
active: false, | |
lastName: 'haha' | |
}, { | |
active: true, | |
lastName: 'dang' | |
}, { | |
active: true, | |
lastName: 'attaboy' | |
}, { | |
active: false, | |
lastName: 'jones' | |
}]; | |
//sort them first by active, then by last name within the active/inactive group | |
var sortByActiveThenLastName = _.sortBy(data, function(item) { | |
return [item.active, item.lastName]; | |
}); | |
console.log(sortByActiveThenLastName); | |
// result --> | |
[{ | |
"active": false, | |
"lastName": "awesome" | |
}, { | |
"active": false, | |
"lastName": "haha" | |
}, { | |
"active": false, | |
"lastName": "johnson" | |
}, { | |
"active": false, | |
"lastName": "jones" | |
}, { | |
"active": true, | |
"lastName": "attaboy" | |
}, { | |
"active": true, | |
"lastName": "dang" | |
}, { | |
"active": true, | |
"lastName": "wow" | |
}] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment