Open In App

Node.js sort() function

sort() is an array function from Node.js that is used to sort a given array. 

Syntax:



array_name.sort()

Parameter: This function does not take any parameter. 

Return type: The function returns the sorted array. 



The program below demonstrates the working of the function: 

Program 1: 




function sortDemo() {
    arr.sort()
    console.log(arr);
}
const arr = [3, 1, 8, 5, 2, 1];
sortDemo();

Output:

[ 1, 1, 2, 3, 5, 8 ]

Program 2: 




function sortDemo() {
    arr.sort()
    console.log(arr);
}
const arr = [-3, 1, 10, 12, 1];
sortDemo();

Output:

[ -3, 1, 1, 10, 12 ]
Article Tags :