Answers for "loadash sort by order of another array"

0

loadash sort by order of another array

var firstArray = ['asdf12','39342aa','12399','129asg',...];

// a collection which has these keys in each object like so:
var collection = [{guid: '39342aa', name: 'John'},{guid: '129asg', name: 'Mary'}, ... ];

const last = collection.length;

var sortedCollection = _.sortBy(collection, function(item){
  return firstArray.indexOf(item.guid) !== -1
  	? firstArray.indexOf(item.guid) : last;
});
Posted by: Guest on August-17-2021

Code answers related to "loadash sort by order of another array"

Code answers related to "Javascript"

Browse Popular Code Answers by Language