例 1 In this example we will create an array and sort it alphabetically: [Ctrl A 全選注:如需引入外部Js需刷新才能執行] 輸出: Jani,Hege,Stale,Kai Jim,Borge,Tove Borge,Hege,Jani,Kai Jim,Stale,Tove範例2 In this example we will create an array and sort it alphabely: [Ctrl A 全選注:如需引入外部Js需刷新才能執行] 輸出: 10,5,40,25,1000,1 1,10,1000,25,40,5請注意,上面的程式碼沒有按照數值的大小對數字進行排序,要實現這一點,就必須使用一個排序函數: [Ctrl A 全選注:如需引入外部Js需刷新才能執行] 輸出: 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()) 10,5,40,25,1000,1 var arr = new Array(6) arr[0] = "10" arr[1] = "5" arr[2] = "40" arr[3] = "25" arr[4] = "1000" arr[5] = "1" document.write(arr + "<br>") document.write(arr.sort()) 1,5,10,25,40,1000