Answers for "sort array of objects by 2 element"

1

sort array of objects by 2 key value

homes.sort(
   function(a, b) {          
      if (a.city === b.city) {
         // Price is only important when cities are the same
         return b.price - a.price;
      }
      return a.city > b.city ? 1 : -1;
   });
Posted by: Guest on June-05-2020
0

sort array of objects based on another array javascript

itemsArray.sort(function(a, b){  
  return sortingArr.indexOf(a) - sortingArr.indexOf(b);
});
Posted by: Guest on October-07-2021
0

javascript sort array of objects multiple fields

array.sort(function(a,b){
  let i = 0, result = 0;
  while(i < sortBy.length && result === 0) {
    result = sortBy[i].direction*(a[ sortBy[i].prop ].toString() < b[ sortBy[i].prop ].toString() ? -1 : (a[ sortBy[i].prop ].toString() > b[ sortBy[i].prop ].toString() ? 1 : 0));
    i++;
  }
  return result;
})
Posted by: Guest on March-02-2021

Code answers related to "sort array of objects by 2 element"

Code answers related to "TypeScript"

Browse Popular Code Answers by Language