JavaScript數組sort()方法排序數組的元素。
語法
array.sort( compareFunction );
下面是參數的詳細信息:
返回值:
返回一個排序的數組
例子:
<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>
這將產生以下結果:
Returned array is : banana,mango,orange,sugar