Sort an array:
The result of fruits will be:
The sort() method sorts the items of an array.
The sort order can be either alphabetic or numeric, and either ascending or descending.
Default sort order is alphabetic and ascending.
Note: When numbers are sorted alphabetically, "40" comes before "5".
To perform a numeric sort, you must pass a function as an argument when calling the sort method.
The function specifies whether the numbers should be sorted ascending or descending.
It can be difficult to understand how this function works, but see the examples at the bottom of this page.
Note: This method changes the original array.
![]()
The sort() method is supported in all major browsers.
| Parameter | Description |
|---|---|
| sortfunction | Optional. A function that defines the sort order |
| Type | Description |
|---|---|
| Array | The Array object, with the items sorted |
| JavaScript Version: | 1.1 |
|---|
Sort numbers (numerically and ascending):
The result of points will be:
Sort numbers (numerically and descending):
The result of points will be:
Sort numbers (alphabetically and descending):
The result of points will be:
JavaScript Array Object
| WEB HOSTING |
|---|
| Best Web Hosting |
| PHP MySQL Hosting |
| Best Hosting Coupons |
| UK Reseller Hosting |
| Cloud Hosting |
| Top Web Hosting |
| $3.98 Unlimited Hosting |
| Premium Website Design |
| WEB BUILDING |
|---|
| XML Editor - Free Trial! |
| FREE Website BUILDER |
| Best Website Templates Top CSS Templates |
| CREATE HTML Websites |
| EASY WEBSITE BUILDER |
| W3SCHOOLS EXAMS |
|---|
|
Get Certified in: HTML, CSS, JavaScript, XML, PHP, and ASP |
| W3SCHOOLS BOOKS |
|---|
|
New Books: HTML, CSS JavaScript, and Ajax |
| STATISTICS |
|---|
|
Browser Statistics Browser OS Browser Display |
| SHARE THIS PAGE |
|---|