Skip to content

Instantly share code, notes, and snippets.

@joewright
Created September 13, 2018 16:34
Show Gist options
  • Save joewright/bbb40c9527f39900b41211303a7f1745 to your computer and use it in GitHub Desktop.
Save joewright/bbb40c9527f39900b41211303a7f1745 to your computer and use it in GitHub Desktop.
_.sortBy example
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