• JavaScript Video Tutorials

JavaScript - Array sort() Method



Description

Javascript array sort() method sorts the elements of an array.

Syntax

Its syntax is as follows −

array.sort( compareFunction );

Parameter Details

compareFunction − Specifies a function that defines the sort order. If omitted, the array is sorted lexicographically.

Return Value

Returns a sorted array.

Example

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>

Output

Returned array is : banana,mango,orange,sugar 
javascript_arrays_object.htm
Advertisements