From http://www.w3schools.com (Copyright Refsnes Data)
JavaScript Array Object
The sort() method is used to sort the elements of an array.
| arrayObject.sort(sortby) |
| Parameter | Description |
|---|---|
| sortby | Optional. Specifies the sort order. Must be a function |
Note: The sort() method will sort the elements alphabetically by default. However, this means that numbers will not be sorted correctly (40 comes before 5). To sort numbers, you must create a function that compare numbers.
Note: After using the sort() method, the array is changed.
Example 1Create an array, and sort it alphabetically:
The output of the code above will be:
Try it yourself » |
Example 2Create an array (with numbers), and sort it correctly:
The output of the code above will be:
Try it yourself » |
JavaScript Array Object
From http://www.w3schools.com (Copyright Refsnes Data)