Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript sort array of objects by number

// 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 of objects javascript by value

const subjects = [
    { "name": "Math", "score": 81 },
    { "name": "English", "score": 77 },
    { "name": "Chemistry", "score": 87 },
    { "name": "Physics", "score": 84 }
];

// Sort in ascending order - by name
subjects.sort((a, b) => (a.name > b.name) ? 1: -1);

console.log(subjects);
Comment

js how to sort array by object value

// @ts-check

(function () {
  const cars = [
    { type: 'Volvo', year: 2016 },
    { type: 'Saab', year: 2001 },
    { type: 'BMW', year: 2010 },
  ];

  /**
   * @param {object[]} arr
   */
  function sortByValue(arr) {
    arr.sort(function (
      /** @type {{ year: number; }} */ a,
      /** @type {{ year: number; }} */ b
    ) {
      return a.year - b.year;
    });
    return arr;
  }
  console.log(sortByValue(cars)); // => [{ type: 'Saab', year: 2001 }, { type: 'BMW', year: 2010 },{ type: 'Volvo', year: 2016 }]
})();
Comment

sort array of objects javascript by properties value

objs.sort((a,b) => (a.last_nom > b.last_nom) ? 1 : ((b.last_nom > a.last_nom) ? -1 : 0))
Comment

PREVIOUS NEXT
Code Example
Javascript :: dark mode with react hooks 
Javascript :: ajax timer 
Javascript :: get file css code with javascript 
Javascript :: How to get previous url in nextjs 
Javascript :: toggle 
Javascript :: date format in javascript 
Javascript :: array in javascript 
Javascript :: array limit js 
Javascript :: get index of first number in string javascript 
Javascript :: captalize first letter javascript 
Javascript :: get largest no in the array javascript 
Javascript :: an arrow function 
Javascript :: shuffle array in javascript 
Javascript :: Regular Expressions: Extract Matches 
Javascript :: sort in array in javascript 
Javascript :: document.getanimation 
Javascript :: for in loop javascript 
Javascript :: currency format 
Javascript :: javascript regex all matches match 
Javascript :: Add additional css class name in react app 
Javascript :: find match in array object js 
Javascript :: double click on element using javascript 
Javascript :: nodejs sequelize find 
Javascript :: bresenham algorithm 
Javascript :: usestate callback 
Javascript :: is there a function like range in react 
Javascript :: foreach in the elements with a data attibute jquery 
Javascript :: firebase get key value 
Javascript :: json type error at login 
Javascript :: for of loop ecmascript 6 
ADD CONTENT
Topic
Content
Source link
Name
1+5 =