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

sort array elements in descending order based on object key

homes.sort((a, b) => parseFloat(a.price) - parseFloat(b.price));
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 :: find function in javascript 
Javascript :: async storage set 
Javascript :: javascript callback function 
Javascript :: javascript output a message into console 
Javascript :: get element by id angular 
Javascript :: age calculator moment js 
Javascript :: filter properties from object javascript 
Javascript :: discord.js purge 
Javascript :: reach last array js 
Javascript :: how to log all messages discord.js 
Javascript :: get the max value from array js 
Javascript :: javascript pass array by value 
Javascript :: js find 
Javascript :: create responsive navbar without javascript 
Javascript :: material ui table row height 
Javascript :: javscript async function 
Javascript :: javascript callback 
Javascript :: javascript xmlhttprequest 
Javascript :: how to write a factorial function in javascript 
Javascript :: pattern printing in javascript 
Javascript :: javascript async await returns undefined 
Javascript :: javascript string to ascii array 
Javascript :: javascript free code editors 
Javascript :: swap first and last element in array javascript 
Javascript :: unlimited number of arguments in function javascript 
Javascript :: relation between leaves nodes and internal nodes in binary tree 
Javascript :: arithmetic expressions in scheme 
Javascript :: js no new line console.log 
Python :: install BeautifulSoup in anaconda 
Python :: plt figsize 
ADD CONTENT
Topic
Content
Source link
Name
1+2 =