Example 1
In this example we'll create an array and sort it alphabetically:
<script type= "Text/javascript" > var arr = new Array (6) arr[0] = "Jani" arr[1] = "Hege" arr[2] = "stale" arr[3] = "Kai Jim" arr[4] = "Borge" arr[5] = "Tove" document.write (arr + "<br>") document.write (Arr.sort ()) </scrip T>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]
Output:
Jani,hege,stale,kai Jim,borge,tove
Borge,hege,jani,kai Jim,stale,tove Example 2
In this example we'll create an array and sort it alphabetically:
<script type= "Text/javascript" > var arr = new Array (6) arr[0] = "ten" arr[1] = "5" arr[2] = "arr[3" = "25" Arr[4] = "1000" arr[5] = "1" document.write (arr + "<br>") document.write (Arr.sort ()) </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]
Output:
10,5,40,25,1000,1
1,10,1000,25,40,5 Note that the code above does not sort the numbers by the size of the numeric value, and to do this, you must use a sort function:
<script type= "Text/javascript" > Function Sortnumber (a,b) {return a-b} var arr = new Array (6) arr[0] = "10 "Arr[1] =" 5 "arr[2] =" arr[3] = "" arr[4] = "1000" arr[5] = "1" document.write (arr + "<br>") document . Write (Arr.sort (sortnumber)) </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]
Output:
10,5,40,25,1000,1
1,5,10,25,40,1000