How to Sort an Array of Integers in Node JS?
Jul 26, 2022
Hi Friends,
Now, let's see tutorial of how to sort an array of integers in node js?. let’s discuss about node js sort array. I would like to show you sort array in integer value node.js. This tutorial will give you simple example of how to sort value in array.
The following example shows the sorting of a list of integer values.
let's see below simple example with output:
Sort Ascending (Lowest to Highest)
index.js
array = [ 1, 6, 4, 9, 3 ]
// sort ascending (1 to X)
sorted = array.sort((a, b) => {
return b - a
})
console.log(sorted);
Output:
[ 1, 3, 4, 6, 9 ]
Sort Descending (Highest to Lowest)
index.js
array = [ 1, 6, 4, 9, 3 ]
// sort ascending (1 to X)
sorted = array.sort((a, b) => {
return a - b
})
console.log(sorted);
Output:
[ 9, 6, 4, 3, 1]
- How to Install Node Js and NPM Ubuntu?
- How to used Google ReCaptcha V3 in Node Js Express?
- How to Send Email with Attachment in Node js?
- How to Insert Data from Form into MySQL Database using Node js Express?
- Install Node Js and NPM Ubuntu 18.04/20.04/22.04 Tutorial
- How to Delete Data by id into MongoDB in Node JS?
- Install Node.js 16/17/18 on CentOS 8 Tutorial
- How to Upload File in MongoDB using Node js Express?