Javascript,Nodejs,MongodDB,WordPress,CSS,PHP

LightBlog

Wednesday, July 7, 2021

How to sort array in ascending and descending order

In this article i am going to sort array element in ascending and descending order using in built javascirpt sort method.

Descending order

let arr = [5, 10, 20, 15, 25, 35];
let sortArray = arr.sort((a, b) => b - a);
console.log(sortArray) // [35, 25, 20, 15, 10, 5]

Ascending order

let arr = [5, 10, 20, 15, 25, 35];
let sortArray = arr.sort((a, b) => a - b);
console.log(sortArray) // [5, 10, 15, 20, 25, 35]

No comments:

Post a Comment