Below is the example of Array sort() method.
- Program 1:
<script>// JavaScript to illustrate sort() functionfunctionfunc() {// Original stringvararr = ["Geeks","for","Geeks"]document.write(arr);document.write("<br>");// Sorting the arraydocument.write(arr.sort());}func();</script>chevron_rightfilter_none - Output:
Geeks,for,Geeks Geeks,Geeks,for
The arr.sort() method is used to sort the array in place in a given order according to the compare() function. If the method is omitted then the array is sorted in ascending order.
Syntax:
arr.sort(compareFunction)
Parameters: This method accept a single parameter as mentioned above and described below:
- compareFunction: This parameters is used to sort the elements according to different attributes and in the different order.
- compareFunction(a,b) < 0
- compareFunction(a,b) > 0
- compareFunction(a,b) = 0
Then a comes before b in the answer.
Then b comes before a in the answer.
Then the order of a and b remains unchanged.
Return value: This method returns the reference of the sorted original array.
Below examples illustrate the JavaScript Array sort() method:
var arr = [2, 5, 8, 1, 4] document.write(arr.sort()); document.write(arr);
Output:
1,2,4,5,8 1,2,4,5,8
var arr = [2, 5, 8, 1, 4]
document.write(arr.sort(function(a, b) {
return a + 2 * b;
}));
document.write(arr);
Output:
2,5,8,1,4 2,5,8,1,4
Code for the above method is provided below:
Program 1:
<script> // JavaScript to illustrate sort() function function func() { //Original string var arr = [2, 5, 8, 1, 4] //Sorting the array document.write(arr.sort()); document.write("<br>"); document.write(arr); } func(); </script> |
Output:
1,2,4,5,8 1,2,4,5,8
Program 2:
<script> // JavaScript to illustrate sort() function function func() { // Original array var arr = [2, 5, 8, 1, 4]; document.write(arr.sort(function(a, b) { return a + 2 * b; })); document.write("<br>"); document.write(arr); } func(); </script> |
Output:
4,1,8,5,2 4,1,8,5,2
Supported Browsers: The browsers supported by JavaScript Array sort() method are listed below:
- Google Chrome
- Microsoft Edge
- Mozilla Firefox
- Safari
- Opera
Recommended Posts:
- Sort an array of objects using Boolean property in JavaScript
- Sort array of objects by string property value in JavaScript
- Sort an Object Array by Date in JavaScript
- How to sort an array of object by two fields in JavaScript ?
- How to Sort Numeric Array using JavaScript ?
- How to sort an array on multiple columns using JavaScript ?
- JavaScript TypeError - Invalid Array.prototype.sort argument
- JavaScript | Sort() method
- Merge Sort for Linked Lists in JavaScript
- JavaScript | typedArray.sort() with Examples
- How to sort strings in JavaScript?
- How to sort rows in a table using JavaScript?
- How to Sort/Order keys in JavaScript objects ?
- How to sort element by numerical value of data attribute using JavaScript ?
- How to compare two JavaScript array objects using jQuery/JavaScript ?
- TypeScript | Array sort() Method
- JavaScript Course | Understanding Code Structure in JavaScript
- Introduction to JavaScript Course | Learn how to Build a task tracker using JavaScript
- JavaScript Course | Loops in JavaScript
- JavaScript Course | Data Types in JavaScript
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


