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

Sorting an array of objects on both ascending and descending order on same click

self.isAscending = true;

self.sortTitle = function () {
 if(self.isAscending){ 
   self.arr= self.arr.sort((a, b) => (a.title > b.title) ? 1 : -1);
 }else{
   self.arr= self.arr.sort((a, b) => (a.title > b.title) ? -1 : 1);
 }
 self.isAscending = !self.isAscending;
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: @jsonignore unrecognized field 
Javascript :: match url regex 
Javascript :: create react app in existing folder 
Javascript :: Convert underscore strings to camel Case 
Javascript :: check if mobile view javascript 
Javascript :: how to disable mouse right click in html page 
Javascript :: go to nextelementsibling js 
Javascript :: vue call function every x seconds 
Javascript :: how to make an express server 
Javascript :: jquery destroy element 
Javascript :: js are you sure alert 
Javascript :: vanilla tilt.js 
Javascript :: Check your Homestead.yaml (or Homestead.json) file, the path to your private key does not exist. 
Javascript :: discord.js ticket system stackoverflow 
Javascript :: save in json file js 
Javascript :: get json data when we get error code in axios 
Javascript :: javascript function that make a choice 
Javascript :: string to char array in javascript 
Javascript :: react native button 
Javascript :: react scroll to top 
Javascript :: js remove spaces 
Javascript :: convert month name to month number in js 
Javascript :: add a trailing slash javascript url 
Javascript :: req.body empty mongodb 
Javascript :: placeholder in angular 9 select with working required 
Javascript :: how use replace in js for all things at once 
Javascript :: javascript folder exists 
Javascript :: jquery scroll when object appear on screen make animation 
Javascript :: uppercase and lowercase letters in js 
Javascript :: Get full year from date object 
ADD CONTENT
Topic
Content
Source link
Name
1+9 =