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

Sorting Array of String, Numbers or Objects in javascript

const stringArr = ["Joe", "Kapil", "Steve", "Musk"]
stringArr.sort();
// Output
(4) ["Joe", "Kapil", "Musk", "Steve"]

stringArr.reverse();
// Output
(4) ["Steve", "Musk", "Kapil", "Joe"]

### Sort Number Array in javascript
const array  = [40, 100, 1, 5, 25, 10];
array.sort((a,b) => a-b);
// Output
(6) [1, 5, 10, 25, 40, 100]

array.sort((a,b) => b-a);
// Output
(6) [100, 40, 25, 10, 5, 1]
Comment

PREVIOUS NEXT
Code Example
Javascript :: delete all document fragments js 
Javascript :: telerik jquery grid set page size 
Javascript :: nextjs youtube embed 
Javascript :: slick slider on change 
Javascript :: remove the bottom selection line from materail ui 
Javascript :: react createelement data attribute 
Javascript :: convert bytes to kb or mb javascript 
Javascript :: javascript object duplicate keys 
Javascript :: OwlCarousel not working after build react js 
Javascript :: puppeteer click is not working 
Javascript :: javascript interview questions geeksforgeeks 
Javascript :: how to add header to axios request 
Javascript :: deep copy array of objects javascript 
Javascript :: how to get multiple values from json array using jq 
Javascript :: react-bootstrap-sweetalert is not running 
Javascript :: suscribe messagechannel lwc 
Javascript :: 3336tfsdfd 
Javascript :: how-can-i-implement-joi-password-complexity-in-joi-validation 
Javascript :: how to create a new window with a specifc link jquery 
Javascript :: js delete without changing index 
Javascript :: multiple variables in one live javascript 
Javascript :: Laravel Vue.js API: axios, FormData() is empty 
Javascript :: how to create your own event emitter in javascript 
Javascript :: How to append variable with anchor element href link in Angularjs 
Javascript :: Easy Angular way to detect if element is in viewport on scroll 
Javascript :: Navigating to another Screen when a button is tapped in React Native 
Javascript :: RegEx Pattern Validations failing on html input 
Javascript :: JavaScript delete atray item 
Javascript :: sinalR 
Javascript :: nodejs passport starter template with username and password 
ADD CONTENT
Topic
Content
Source link
Name
3+9 =