Answers for "order array by value js"

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
-1

put array in alphabetical order

var fruits = ["Banana", "Orange", "Apple", "Mango"];
fruits.sort();
Posted by: Guest on February-09-2020
0

sort array by a value js

arr.sort((a, b) => (a.nameofKeyFromArray < b.nameofKeyFromArray ? -1 : Number(a.nameofKeyFromArray > b.nameofKeyFromArray)));
Posted by: Guest on October-01-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language