Skip to content
Related Articles

Related Articles

Node.js | sort() function
  • Last Updated : 30 Aug, 2019
GeeksforGeeks - Summer Carnival Banner

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 ]

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :