JavaScript sort () Method
Examples
Sorting an array:
fruits.sort();
fruits output:
try it"
Definition and Usage
sort () method is used to sort the elements of the array.
Sort order may be a letter or number, and press the ascending or descending order.
The default sort order is alphabetical in ascending order.
Note: When the numbers are in alphabetical order "40" will be ranked in the "5" front.
Using digital ordering, you must as a parameter called through a function.
Function Specifies the number is in ascending or descending order.
They say it may be difficult to understand, you can learn more about it at the bottom of this page instances.
NOTE: This method changes the original array!.
Browser Support
All major browsers support sort ().
grammar
Parameter Values
参数 | 描述 |
---|---|
sortfunction | 可选。规定排序顺序。必须是函数。 |
return value
Type | 描述 |
---|---|
Array | 对数组的引用。请注意,数组在原数组上进行排序,不生成副本。 |
technical details
JavaScript Version: | 1.1 |
---|
More examples
Examples
Number Sequencing (numeric and ascending order):
points.sort(function(a,b){return a-b});
fruits output:
try it"
Examples
Number Sequencing (digital and descending):
points.sort(function(a,b){return b-a});
fruits output:
try it"
Examples
Number Sequencing (letters and descending order):
fruits.sort();
fruits.reverse();
fruits output:
try it"