本文實例講述了JavaScript實現數字數組按照倒序排列的方法。分享給大家供大家參考。具體如下:
下面的代碼演示了JS數組如何通過sort對數字類型的數組進行倒序排序
<!DOCTYPE html><html><body><p id="demo">Click the button to sort the array.</p><button onclick="myFunction()">Try it</button><script>function myFunction(){var points = [40,100,1,5,25,10];points.sort(function(a,b){return b-a});var x=document.getElementById("demo");x.innerHTML=points;}</script></body></html>上面的代碼輸出結果如下
100,40,25,10,5,1
希望本文所述對大家的javascript程序設計有所幫助。
新聞熱點
疑難解答