Javascript array sort() method sorts the elements of an array.
Its syntax is as follows −
array.sort( compareFunction );
compareFunction − Specifies a function that defines the sort order. If omitted, the array is sorted lexicographically.
Returns a sorted array.
Try the following example.
<html> <head> <title>JavaScript Array sort Method</title> </head> <body> <script type="text/javascript"> var arr = new Array("orange", "mango", "banana", "sugar"); var sorted = arr.sort(); document.write("Returned string is : " + sorted ); </script> </body> </html>
Returned array is : banana,mango,orange,sugar