How to Sort an Array of Integers in Node JS?

28-Jul-2022

.

Admin

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]

I hope it can help you...

#Node JS