Answers for "javascript sort array by value"

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

how to sort an array by value in javascript

//ascending order
arr.sort(function(a, b){return a-b});
arr.sort((a, b) => a - b);
//descending order 
arr.sort(function(a, b){return b - a});
arr.sort((a, b) => b - a);
Posted by: Guest on March-29-2021
13

javascript orderby

var items = [
  { name: 'Edward', value: 21 },
  { name: 'Sharpe', value: 37 },
  { name: 'And', value: 45 },
  { name: 'The', value: -12 },
  { name: 'Magnetic', value: 13 },
  { name: 'Zeros', value: 37 }
];

// sort by value
items.sort(function (a, b) {
  return a.value - b.value;
});

// sort by name
items.sort(function(a, b) {
  var nameA = a.name.toUpperCase(); // ignore upper and lowercase
  var nameB = b.name.toUpperCase(); // ignore upper and lowercase
  if (nameA < nameB) {
    return -1;
  }
  if (nameA > nameB) {
    return 1;
  }

  // names must be equal
  return 0;
});
Posted by: Guest on May-30-2020
3

sorting of arraray's number element in javascript

let numbers = [0, 1, 2, 3, 10, 20, 30];
numbers.sort((a, b) => a - b);

console.log(numbers);
Posted by: Guest on April-17-2020
4

javascript sort

homes.sort((a, b) => parseFloat(a.price) - parseFloat(b.price));
Posted by: Guest on March-17-2020
0

order array js by a value

arr.sort((a, b) => a.name > b.name ? 1 : -1);
Posted by: Guest on September-23-2021

Code answers related to "javascript sort array by value"

Code answers related to "Javascript"

Browse Popular Code Answers by Language