Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

sort array of object by another value array in javascript

var sortOrder = [2,3,1,4],
    items     = [{id: 1}, {id: 2}, {id: 3}, {id: 4}];

items.sort(function (a, b) {
  return sortOrder.indexOf(a.id) - sortOrder.indexOf(b.id);
});
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #sort #array #object #array #javascript
ADD COMMENT
Topic
Name
2+2 =