Answers for "js sorting array of objects"

26

sort array of object js

const books = [
  {id: 1, name: 'The Lord of the Rings'},
  {id: 2, name: 'A Tale of Two Cities'},
  {id: 3, name: 'Don Quixote'},
  {id: 4, name: 'The Hobbit'}
]

books.sort((a,b) => (a.name > b.name) ? 1 : ((b.name > a.name) ? -1 : 0));
Posted by: Guest on April-08-2021
19

javascript sort array with objects

var array = [
  {name: "John", age: 34},
  {name: "Peter", age: 54},
  {name: "Jake", age: 25}
];

array.sort(function(a, b) {
  return a.age - b.age;
}); // Sort youngest first
Posted by: Guest on May-20-2020
2

sort array with objects

const list = [
  { color: 'white', size: 'XXL' },
  { color: 'red', size: 'XL' },
  { color: 'black', size: 'M' }
]

var sortedArray = list.sort((a, b) => (a.color > b.color) ? 1 : -1)

// Result:
//sortedArray:
//{ color: 'black', size: 'M' }
//{ color: 'red', size: 'XL' }
//{ color: 'white', size: 'XXL' }
Posted by: Guest on June-06-2021
5

sort array of objects javascript

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

sort objects in array js

function createDropDowns() {
    for (const [key, val] of Object.entries($pgns["nmea2000"])) {
      pgnList = [...pgnList, val];
      fieldsList[val.title] = val.fields;
      fieldsListOut[val.title] = val.fields;
    }
    pgnList.sort((a, b) =>
      a.title > b.title ? 1 : b.title > a.title ? -1 : 0
    );
}

pgnList = [
  	{pgn: '065280', manufacturer: '161', title: 'Proprietary - Set Serial Number', fields: Array(6), route: '/nmea2000/data/{busNumber}/by_instance/{instance}/065280/{field}'}
	{pgn: '065280', manufacturer: '257', title: 'Engine Status Parameters, Dynamic', fields: Array(10), route: '/nmea2000/data/{busNumber}/by_instance/{instance}/065280/{field}'}
	{pgn: '065284', manufacturer: '257', title: 'Display Communication Status', fields: Array(12), route: '/nmea2000/data/{busNumber}/by_instance/{instance}/065284/{field}'}
	{pgn: '065289', manufacturer: '161', function: '1', title: 'Proprietary - Set Volume and Capacity', fields: Array(8), …}
	{pgn: '065289', manufacturer: '161', function: '2', title: 'Proprietary' }
]
Posted by: Guest on September-30-2021
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

Code answers related to "js sorting array of objects"

Code answers related to "Javascript"

Browse Popular Code Answers by Language