zoukankan      html  css  js  c++  java
  • javascript数组排序-----1

     1 <!DOCTYPE html>
     2 <html lang="en">
     3 <head>
     4     <meta charset="UTF-8">
     5     <title>javascript数组去重算法-----3</title>
     6 </head>
     7 <body>
     8     <script>
     9 var arrDemo = new Array();
    10 
    11  arrDemo[0] = 10;
    12  arrDemo[1] = 50;
    13  arrDemo[2] = 51;
    14  arrDemo[3] = 100;
    15 
    16  arrDemo.sort(); //调用sort方法后,数组本身会被改变,即影响原数组
    17 
    18 console.log(arrDemo);//10,100,50,51 默认情况下sort方法是按ascii字母顺序排序的,而非我们认为是按数字大小排序
    19 
    20  arrDemo.sort(function(a,b){return a>b?1:-1});//从小到大排序
    21 
    22 console.log(arrDemo);//10,50,51,100
    23 
    24  arrDemo.sort(function(a,b){return a<b?1:-1});//从大到小排序
    25 
    26  console.log(arrDemo);
    27     </script>
    28 </body>
    29 </html>
    坚持下去就能成功
  • 相关阅读:
    isequal 和startswith 使用
    UVa10340
    UVa1368
    UVa455
    UVa1225
    UVa1586
    UVa 1585
    UVa10082
    UVa272
    NYOJ1
  • 原文地址:https://www.cnblogs.com/suoking/p/4865545.html
Copyright © 2011-2022 走看看