I have an object that is being returned from a database like this: [{id:1},{id:2},{id:3}]. I have another array which specified the order the first array should be
[{id:1},{id:2},{id:3}]
function sortArrayByOrderArray(arr, orderArray) { return arr.sort(function(e1, e2) { return orderArray.indexOf(e1.id) - orderArray.indexOf(e2.id); }); } console.log(sortArrayByOrderArray([{id:1},{id:2},{id:3}], [2,3,1]));