数组对象排序方法及默认地址和时间戳排序

---恢复内容开始---

数组排序的话最常用的就是array.sort()这个方法了。

sort()里面传入一个方法来比较大小的值;

function compare(a,b) {
    return a-b;
} 

对象进行比较的话:按照时间排序

function minor (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 ("error");  
                    }  
                }  
            }
            var employees=[];  
            employees[0]={name:"George", time:1494581848, default:0}  
            employees[1]={name:"Edward", time:2494581000, default:0}
            employees[2]={name:"555", default:0}
            employees[3]={name:"Christine", time:2491258100, default:1}  
            employees[4]={name:"Sarah", time:123125818, default:0}
            employees[5]={name:"liu", default:0}
            employees.sort(minor("time"));

还有一种方法是当两个值相等时,拿另一个参数作为比较值

var by = function(name,minor){  
                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 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 ("error");  
                    }  
                }  
            }
            function minor (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 ("error");  
                    }  
                }  
            }
            var employees=[];  
            employees[0]={name:"George", time:1494581848, default:0}  
            employees[1]={name:"Edward", time:2494581000, default:0}
            employees[2]={name:"555", default:0}
            employees[3]={name:"Christine", time:2491258100, default:1}  
            employees[4]={name:"Sarah", time:123125818, default:0}
            employees[5]={name:"liu", default:0}
            employees.sort(by("default","time"));

然后我最近要做的是默认地址排在第一位,之后的按照时间排序

function reviseSort (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 ("error");  
                    }  
                }  
            }

var data = [{name:"George", time:1494581848, default:0},{name:"Edward", time:2494581000, default:0},{name:"Christine", time:2491258100, default:1}, {name:"Sarah", time:123125818, default:0}]

data.sort(self.reviseSort("default"));//按照默认地址排序
var data_default = [].concat(data[0]);//获取默认地址
data.splice(0,1);//删除默认地址
data.sort(self.reviseSort("created"));//按照时间排序
Array.prototype.push.apply(data_default, data);//把data数组插入data_default

 

 

 

 

---恢复内容结束---

posted @ 2017-05-18 11:10  DOM先生  阅读(1587)  评论(0编辑  收藏  举报