OK so given this input (other properties have been stripped for brevity):
var names = [{
name: 'Michael'
}, {
name: 'Liam'
}, {
name: 'Jake'
}, {
name: 'Dave'
}, {
name: 'Adam'
}];
I'd like to sort them by another array's indices, and if they aren't in that array, sort alphabetically.
var list = ['Jake', 'Michael', 'Liam'];
Giving me an output of:
Jake, Michael, Liam, Adam, Dave
I've tried using lo-dash but it's not quite right:
names = _.sortBy(names, 'name');
names = _.sortBy(names, function(name) {
var index = _.indexOf(list, name.name);
return (index === -1) ? -index : 0;
});
as the output is:
Jake, Liam, Michael, Adam, Dave
Any help would be really appreciated!
Adam
thenDave
, how did you sort that out? – elclanrs Nov 25 '13 at 0:05sort()
although I was already using lodash in the project so it doesn't matter to me to use a native method over the (in my opinion, neater) lodash one. – Ben Nov 25 '13 at 18:41