Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript sort array of objects ascending and descending order

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

descending order in objects in js

starWarsPeople.map(e=>{
    return e
  }).sort((a,b)=> b.height - a.height);
Comment

javascript sort array of objects by key value ascending and descending order

 function compareValues(key, order = 'asc') {
  return function innerSort(a, b) {
    if (!a.hasOwnProperty(key) || !b.hasOwnProperty(key)) {
      // property doesn't exist on either object
      return 0;
    }

    const varA = (typeof a[key] === 'string')
      ? a[key].toUpperCase() : a[key];
    const varB = (typeof b[key] === 'string')
      ? b[key].toUpperCase() : b[key];

    let comparison = 0;
    if (varA > varB) {
      comparison = 1;
    } else if (varA < varB) {
      comparison = -1;
    }
    return (
      (order === 'desc') ? (comparison * -1) : comparison
    );
  };
}
Comment

javascript sort object by value descending

javascript sort object by value descending


starWarsPeople.map(e=>{
    return e
  }).sort((a,b)=>{
    if (a.height-b.height>1) {return -1;}
    else if (a.height-b.height<1) {return 1;}
    else {return 0;}
  });
Comment

PREVIOUS NEXT
Code Example
Javascript :: firebase realtime database increment value 
Javascript :: how to read excel file in nodejs 
Javascript :: javascript array last element get 
Javascript :: javascript set header text 
Javascript :: react portal example 
Javascript :: how to convert json to object 
Javascript :: react native generate signed apk getting older version 
Javascript :: check if property has value in array javascript 
Javascript :: react spring transition animations 
Javascript :: JSON.parse() error 
Javascript :: add role to channel discord.js 
Javascript :: rest operator in javascript 
Javascript :: sequelize find query to return raw data in json object format 
Javascript :: React Redux store exemple 
Javascript :: maximum number of an array 
Javascript :: javascript camelcase regex 
Javascript :: Material-ui add circle icon 
Javascript :: callbacks in jquery 
Javascript :: npm mongoose findorcreate 
Javascript :: how to check if input is valid javascript 
Javascript :: fixed header on scroll vuejs 
Javascript :: how to create module in react 
Javascript :: json parse vs json stringify 
Javascript :: how to add multiple videos in html5 with javascript 
Javascript :: showdown react 
Javascript :: does javascript buelt applications 
Python :: pygame disable message 
Python :: opencv show image jupyter 
Python :: error tokenizing data. c error 
Python :: python read json file 
ADD CONTENT
Topic
Content
Source link
Name
3+5 =