zoukankan      html  css  js  c++  java
  • 引用类型 重排序方法

    重排序方法:reverse()和sort()

    reverse()方法:会反转数组项的顺序

    sort()方法:按升序排列------即小在前大在后

    sort()方法比较的是字符串   eg:"ab">"a"    "aa"<"ab"

    var arr=[0,10,5,15,20];

    arr.sort();

    alert(arr);//[0,10,15,20,5]

    在比较时sort()方法:可以用升序    也可以用降序排列

    升序:

    function compare(value1, value2) {
    if (value1 < value2) {
    return -1;
    } else if (value1 > value2) {
    return 1;
    } else {
    return 0;
    }
    }
     
    var values = [0, 1, 5, 10, 15];
    values.sort(compare);
    alert(values); //0,1,5,10,15
     
     
    降序:
    function compare(value1, value2) {
    if (value1 < value2) {
    return 1;
    } else if (value1 > value2) {
    return -1;
    } else {
    return 0;
    }
    }
    var values = [0, 1, 5, 10, 15];
    values.sort(compare);
    alert(values); // 15,10,5,1,0
     
     
    观察后发现 sort()在比较时是根据-1,1,0来决定它的排序的
     
  • 相关阅读:
    Android中得到布局文件对象有三种方式
    android中的键值对
    .length()与.length与.size()
    异常处理
    Python操作Excel
    写一个简单的爬虫(博客)
    开发一个登录接口(Mysql)
    常用模块
    内置函数
    装饰器
  • 原文地址:https://www.cnblogs.com/zmlAliIqsgu/p/12129392.html
Copyright © 2011-2022 走看看