sort()

var m = ['aa', 'bb', 'a', 4, 8, 15, 16, 23, 42]; 
    m.sort(function (a, b) { 
       if (a === b) { 
          return 0; 
  } 
       if (typeof a === typeof b) { 
          return a < b ? -1 : 1; 
       } 
       return typeof a < typeof b ? -1 : 1; 
    }); 
    // m 是

 [4, 8, 15, 16, 23, 42, 'a', 'aa', 'bb'] 

对象数组排序
 // by 函数接受一个成员名字字符串做为参数,
    // 并返回一个可以用来对包含该成员的对象数组的进行排序的比较函数
    var by = function (name) {
        return function (o, p) {
            var a, b;
            if (typeof o === 'object' && typeof p === 'object' && o && p) {
                a = o[name];
                b = p[name];
                if (a === b) {
                    return 0;
                }
                if (typeof a === typeof b) {
                    return a < b ? -1 : 1;
                }
                return typeof a < typeof b ? -1 : 1;
            } else {
                throw {
                    name: 'Error',
                    message: 'Expected an object when sorting by ' + name
                };
            }
        };
    }
    var myPeople = [
        {first: 'Joe',      last: 'Beasser'},
        {first: 'Moe',      last: 'Howard'},
        {first: 'Joe',      last: 'DeRita'},
        {first: 'Shemp',    last: 'Howard'},
        {first: 'Larry',    last: 'Fine'},
        {first: 'Curly',    last: 'Howard'}
    ];
    myPeople.sort(by('first'));
    /*
    [
    {first: "Curly",    last: "Howard"},
    {first: "Joe",      last: "Beasser"},
    {first: "Joe",      last: "DeRita"},
    {first: "Larry",    last: "Fine"},
    {first: "Moe",      last: "Howard"},
    {tfirst: "Shemp",   last: "Howard"}
    ]
 */
下面的调用:
    myPeople.sort(by('last')).sort(by('first'));
    /*
    [
    {"first": "Curly",  "last": "Howard"},
    {"first": "Joe",    "last": "Beasser"},
    {"first": "Joe",    "last": "DeRita"},
    {"first": "Larry",  "last": "Fine"},
    {"first": "Moe",    "last": "Howard"},
    {"first": "Shemp",  "last": "Howard"}]
    */

不能保证产生正确的序列。如果你想基于多个键值进行排序,你需要再次做更多的工作。我们可以修改by函数,让其可以接受第2个参数,当主要的键值产生一个匹配的时候,另一个compare方法将被调用以决出高下。

    // by 函数接受一个成员名字字符串和一个可选的次要比较函数做为参数,
    // 并返回一个可以用来对包含该成员的对象数组进行排序的比较函数。
    // 当 o[name] 和 p[name] 相等时,次要比较函数被用来决出高下。
    var by = function (name, minor) {
        return function (o, p) {
            var a, b;
            if (o && p && typeof o === 'object' && typeof p === 'object') {
                a = o[name];
                b = p[name];
                if (a === b) {
                    return typeof minor === 'function' ? minor(o, p) : 0;
                }
                if (typeof a === typeof b) {
                    return a < b ? -1 : 1;
                }
                return typeof a < typeof b ? -1 : 1;
            } else {
                throw {
                    name: 'Error',
                    message: 'Expected an object when sorting by ' + name
                };
            }
        };
    }
    myPeople.sort(by('last', by('first')));
    [
    {"first": "Joe",    "last": "Beasser"},
    {"first": "Joe",    "last": "DeRita"},
    {"first": "Larry",  "last": "Fine"},
    {"first": "Curly",  "last": "Howard"},
    {"first": "Moe",    "last": "Howard"},
    {"first": "Shemp",  "last": "Howard"}
    ]


猜你喜欢

转载自blog.csdn.net/weixin_38174062/article/details/80963010