Lodash sort collection based on external array
问题 I have an array with keys like so: ['asdf12','39342aa','12399','129asg',...] and a collection which has these keys in each object like so: [{guid: '39342aa', name: 'John'},{guid: '129asg', name: 'Mary'}, ... ] Is there a fast way to sort the collection based on the order of keys in the first array? 回答1: var sortedCollection = _.sortBy(collection, function(item){ return firstArray.indexOf(item.guid) }); 回答2: Input: var data1 = ['129asg', '39342aa']; var data2 = [{ guid: '39342aa', name: 'John'