Skip to content
Related Articles

Related Articles

Node.js sort() function

View Discussion
Improve Article
Save Article
  • Last Updated : 14 Oct, 2021

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 takes 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);
}
var arr=[ 3, 1, 8, 5, 2, 1];
sortDemo();


Output:

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

Program 2:




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


Output:

[ -3, 1, 1, 10, 12 ]
My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!