GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.geeksforgeeks.org — JavaScript Array sort() Method - GeeksforGeeks JavaScript Array sort() MethodLast Updated : 16 Jan, 2026 The sort() method in JavaScript is used to arrange array elements in a specific order, usually alphabetically or in ascending order. It modifies the original array and returns the sorted result.
Sorting Array of Strings
Syntax arr.sort(compareFunction);Parameters
Return value: This method returns the reference of the sorted original array. Sorting a Numeric Array in JavascriptAscending OrderArray.sort() sorts the elements in lexicographical order whether a string or number.
To sort the numeric array property we have to pass a comparator function.
Descending Orderchange the comparator function to sort the array in descending order.
Please go through this How to Sort Numeric Array using JavaScript?, to know how the JavaScript array sort function works. Sort Array of Strings in Reverse OrderWe can use the string.localeCompare() method in the comparator function to sort in reverse order.
We can also use the array.reverse() method to sort the array in descending order.
Output [ 'JS', 'HTML', 'CSS' ] Sorting Array of ObjectsThe array of objects can be sorted on the basis of property values.
Sort StabilitySort stability means that when sorting, if two items have the same value, their order stays the same as in the original list. A stable sort keeps the order of equal items unchanged during sorting.
This article explains the sort stability of the JavaScript sort(). Comment Explore |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |