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

javascript sort array of objects by key value

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

sort object dictionary javscript

var dict = {
  "x": 1,
  "y": 6,
  "z": 9,
  "a": 5,
  "b": 7,
  "c": 11,
  "d": 17,
  "t": 3
};

// Create items array
var items = Object.keys(dict).map(function(key) {
  return [key, dict[key]];
});

// Sort the array based on the second element
items.sort(function(first, second) {
  return second[1] - first[1];
});

// Create a new array with only the first 5 items
console.log(items.slice(0, 5));
Comment

PREVIOUS NEXT
Code Example
Javascript :: js string have number js 
Javascript :: mean vs mern stack 
Javascript :: how to link a photo in expo react native 
Javascript :: access css property using javascript 
Javascript :: settimeout javascript see how much time is left 
Javascript :: how to define state in react function 
Javascript :: expo react native 
Javascript :: cors express tutorial 
Javascript :: electron jquery 
Javascript :: how to validate the radio button using jquery 
Javascript :: run jest on single file 
Javascript :: 2d array to 1d array javascript 
Javascript :: typing animation in js 
Javascript :: image animation jquery 
Javascript :: user api testing 
Javascript :: What is data modeling in MongoDB 
Javascript :: node array 
Javascript :: node.js - How do I convert an image to a base64-encoded data URL 
Javascript :: vue.js slots 
Javascript :: jquery empty 
Javascript :: retunr empty new promise 
Javascript :: how to print to screen in javascript 
Javascript :: laravel return validation errors ajax 
Javascript :: bigger or equal javascript 
Javascript :: access selected option in jquery 
Javascript :: when modal close event 
Javascript :: queryselector name attribute 
Javascript :: localstorage clear item 
Javascript :: react fetch custom hook 
Javascript :: How to Use the toUpperCase() String Method in javascript 
ADD CONTENT
Topic
Content
Source link
Name
2+3 =