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 :: run react app in react 18 
Javascript :: fetchData from json file 
Javascript :: vuex v-model 
Javascript :: javascript number methods 
Javascript :: p5.js change button position 
Javascript :: printf javasscript 
Javascript :: express search query template 
Javascript :: react-native android 
Javascript :: ajax authorization header token 
Javascript :: pagination jsonplaceholder 
Javascript :: jQuery UI Sortable, then write order into a database 
Javascript :: javascript get last item in array 
Javascript :: remove white space from string in js 
Javascript :: get minutes and seconds from seconds 
Javascript :: calling javascript functions from unity scripts 
Javascript :: js array sum 
Javascript :: color console 
Javascript :: express start template 
Javascript :: Using webpack 5. Reason: future.webpack5 option enabled https://nextjs.org/docs/messages/webpack5 
Javascript :: Triplets summing up to a target value 
Javascript :: js promis with ajax 
Javascript :: immediately invoked function in javascript 
Javascript :: javascript clone array of object 
Javascript :: js array sort 
Javascript :: style react background 
Javascript :: code for adding new elements in javascriipt js 
Javascript :: javascript convert string to number or integer 
Javascript :: statusbar.sethidden(true) in react native 
Javascript :: select add option javascript 
Javascript :: generate 50 random numbers between 1 and 500 in javascript 
ADD CONTENT
Topic
Content
Source link
Name
5+1 =