Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript sort array of objects by key value

// Price Low To High
array?.sort((a, b) => (a.price > b.price ? 1 : -1))
// Price High To Low
array?.sort((a, b) => (a.price > b.price ? -1 : 1))
// Name A to Z
array?.sort((a, b) => (a.name > b.name ? 1 : 1))
// Name Z to A
array?.sort((a, b) => (a.name > b.name ? -1 : 1))
// Sort by date
array.sort((a,b) =>  new Date(b.date) - new Date(a.date));
Comment

sort object by key value js

//For Ascending
a.sort((a,b)=> (a.name > b.name ? 1 : -1))

//For Descending
a.sort((a,b)=> (a.name < b.name ? 1 : -1))
Comment

javascript sort array of objects by key value

arr.sort((x, y) => x.distance - y.distance);
Comment

sort array of objects javascript by key value

myArray.sort(function(a, b) {
    return a.distance - b.distance;
});
Comment

javascript sort array of objects by value of key in object

//Sort Objects in array Descending order
const sortDesc(a,b) => {
  return b.timestamp - a.timestamp;
}

//Sort Objects in array Ascending order
const sortAsc(a,b) => {
  return a.timestamp - b.timestamp;
}

<Array>.sort(sortAsc);
<Array>.sort(sortDesc);
Comment

PREVIOUS NEXT
Code Example
Javascript :: how to deep copy an object in javascript 
Javascript :: node.js web server 
Javascript :: jquery timepicker 
Javascript :: how to add a picker in expo 
Javascript :: use these instead of a for loop javascript 
Javascript :: how to change the first 3 letters from a string toupper case 
Javascript :: javascript round to 7 decimal places 
Javascript :: react native password strength 
Javascript :: remover ultimo character string javascript 
Javascript :: javascript factorial of a number 
Javascript :: js password check 
Javascript :: json parse returns object 
Javascript :: fs.readdir callback function 
Javascript :: javaScript getDay() Method 
Javascript :: how to find the sum of array using JavaScript 
Javascript :: Error: "line" is not a registered controller 
Javascript :: javascript base64 decode 
Javascript :: javascript promise 
Javascript :: format to precision 2 javascript if double 
Javascript :: separate last character string javascript 
Javascript :: mongoose deprecation warning 
Javascript :: unique array in javascript 
Javascript :: usecontext react 
Javascript :: NextJS + Material-UI - Warning: Prop className did not match 
Javascript :: js array 0 to n 
Javascript :: isprime js 
Javascript :: javascript mod 
Javascript :: int val javascript 
Javascript :: express referrer 
Javascript :: input events 
ADD CONTENT
Topic
Content
Source link
Name
6+3 =