Answers for "sort array of objects in descending order javascript"

8

javascript sort array of objects by key value

arr.sort((x, y) => x.distance - y.distance);
Posted by: Guest on March-15-2020
1

descending order in objects in js

starWarsPeople.map(e=>{
    return e
  }).sort((a,b)=> b.height - a.height);
Posted by: Guest on May-20-2021
-1

javascript sort array of objects by key value ascending and descending order

function compareValues(key, order = 'asc') {
  return function innerSort(a, b) {
    if (!a.hasOwnProperty(key) || !b.hasOwnProperty(key)) {
      // property doesn't exist on either object
      return 0;
    }

    const varA = (typeof a[key] === 'string')
      ? a[key].toUpperCase() : a[key];
    const varB = (typeof b[key] === 'string')
      ? b[key].toUpperCase() : b[key];

    let comparison = 0;
    if (varA > varB) {
      comparison = 1;
    } else if (varA < varB) {
      comparison = -1;
    }
    return (
      (order === 'desc') ? (comparison * -1) : comparison
    );
  };
}
Posted by: Guest on June-28-2020
0

javascript sort object by value descending

javascript sort object by value descending


starWarsPeople.map(e=>{
    return e
  }).sort((a,b)=>{
    if (a.height-b.height>1) {return -1;}
    else if (a.height-b.height<1) {return 1;}
    else {return 0;}
  });
Posted by: Guest on May-17-2021

Code answers related to "sort array of objects in descending order javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language