Answers for "how to sort object values in javascript"

15

sort by object property javascript

let list = [
  {
      name: "world"
  },
  {
      name: "hello",
  },
];

// This doesn't account for if names are the same between objects
let x = list.sort((a, b) => (a.name > b.name ? 1 : -1));

console.log(x);

/*
[
  {
      name: "hello",
  },
  {
      name: "world"
  },
];
*/
Posted by: Guest on August-23-2020
7

sort array of objects javascript by value

let orders = [
  { 
    order: 'order 1', date: '2020/04/01_11:09:05'
  },
  { 
    order: 'order 2', date: '2020/04/01_10:29:35'
  },
  { 
    order: 'order 3', date: '2020/04/01_10:28:44'
  }
];


console.log(orders);

orders.sort(function(a, b){
  let dateA = a.date.toLowerCase();
  let dateB = b.date.toLowerCase();
  if (dateA < dateB) 
  {
    return -1;
  }    
  else if (dateA > dateB)
  {
    return 1;
  }   
  return 0;
});

console.log(orders);
Posted by: Guest on April-01-2020
3

javascript sort object

var maxSpeed = {
    car: 300, 
    bike: 60, 
    motorbike: 200, 
    airplane: 1000,
    helicopter: 400, 
    rocket: 28800
};
var sortable = [];
for (var vehicle in maxSpeed) {
    sortable.push([vehicle, maxSpeed[vehicle]]);
}

sortable.sort(function(a, b) {
    return a[1] - b[1];
});

//[["bike", 60], ["motorbike", 200], ["car", 300],
//["helicopter", 400], ["airplane", 1000], ["rocket", 28800]]
Posted by: Guest on April-19-2020
0

sort() object values javascript

list.sort((a, b) => (a.color > b.color) ? 1 : (a.color === b.color) ? ((a.size > b.size) ? 1 : -1) : -1 )
Posted by: Guest on May-18-2020

Code answers related to "how to sort object values in javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language