Open In App

Node.js sort() function

Last Updated : 30 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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: 

javascript




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: 

javascript




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


Output:

[ -3, 1, 1, 10, 12 ]

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads