好吧,我有一个带有随机值的数组对象, 实施例
var arr = [
{ id:1001, date:"20-02-2014", Name: 'demo1' },
{ id:1004, date:"13-02-2014", Name: 'demo0' },
{ id:1000, date:"10-02-2014", Name: 'demo14' },
{ id:1004, date:"16-02-2014", Name: 'demo10' },
{ id:1006, date:"22-02-2014", Name: 'demo111' },
{ id:1003, date:"28-02-2014", Name: 'demo16' },
{ id:1000, date:"28-01-2014", Name: 'demo12' },
{ id:1004, date:"28-01-2014", Name: 'demo01' },
{ id:1000, date:"08-01-2014", Name: 'demo41' },
{ id:1006, date:"08-01-2014", Name: 'demo91' }
]
我想首先按键id
&然后按键date
as,
输出:
sorted_arr = [
{"id":1000,"date":"08-01-2014","Name":"demo41"}, //group1
{"id":1000,"date":"28-01-2014","Name":"demo12"}, //group1
{"id":1000,"date":"10-02-2014","Name":"demo14"}, //group1
{"id":1001,"date":"20-02-2014","Name":"demo1"}, //group2
{"id":1003,"date":"28-02-2014","Name":"demo16"}, //group3
{"id":1004,"date":"28-01-2014","Name":"demo01"}, //group4
{"id":1004,"date":"13-02-2014","Name":"demo0"}, //group4
{"id":1004,"date":"16-02-2014","Name":"demo10"}, //group4
{"id":1006,"date":"08-01-2014","Name":"demo91"} //group5
{"id":1006,"date":"22-02-2014","Name":"demo111"} //group5
]
我尝试了几种通用代码来排序,
// generic comparison function
cmp = function(x, y){
return x > y ? 1 : x < y ? -1 : 0;
};
arr.sort(function(a, b){
return cmp(
[cmp(a.id, b.id), cmp(a.date, b.date)],
[cmp(b.id, a.id), cmp(b.date, a.date)]
);
});
我提到了几个例子SO Example,但没有得到预期的输出。请建议我最好的方法来获得这个。
答案 0 :(得分:8)
无需创建Date
个对象,只需将日期字符串重新排序为可排序的字符串,例如
此示例假定您的日期格式为DD-MM-YYYY
,并为日期排序创建YYYYMMDD
。
的Javascript
var arr = [
{ id:1001, date:"20-02-2014", Name: 'demo1' },
{ id:1004, date:"13-02-2014", Name: 'demo0' },
{ id:1000, date:"10-02-2014", Name: 'demo14' },
{ id:1004, date:"16-02-2014", Name: 'demo10' },
{ id:1006, date:"22-02-2014", Name: 'demo111' },
{ id:1003, date:"28-02-2014", Name: 'demo16' },
{ id:1000, date:"28-01-2014", Name: 'demo12' },
{ id:1004, date:"28-01-2014", Name: 'demo01' },
{ id:1000, date:"08-01-2014", Name: 'demo41' },
{ id:1006, date:"08-01-2014", Name: 'demo91' }
];
var sorted = arr.sort(function (a, b) {
return a.id - b.id || a.date.split('-').reverse().join('') - b.date.split('-').reverse().join('');
});
sorted.forEach(function (element) {
console.log(JSON.stringify(element));
});
输出
{"id":1000,"date":"08-01-2014","Name":"demo41"} {"id":1000,"date":"28-01-2014","Name":"demo12"} {"id":1000,"date":"10-02-2014","Name":"demo14"} {"id":1001,"date":"20-02-2014","Name":"demo1"} {"id":1003,"date":"28-02-2014","Name":"demo16"} {"id":1004,"date":"28-01-2014","Name":"demo01"} {"id":1004,"date":"13-02-2014","Name":"demo0"} {"id":1004,"date":"16-02-2014","Name":"demo10"} {"id":1006,"date":"08-01-2014","Name":"demo91"} {"id":1006,"date":"22-02-2014","Name":"demo111"}上
如果对混合日期格式有任何疑虑,如@xdazz所讨论的那样,那么您可以通过自己检查填充来改进这一点。按日期排序时,以下内容将创建格式“YYYYYYMMDD”。在这个例子中没有必要使用额外的年份填充,因为我正在取值的数值差异,但如果你选择比较字符串那么它很重要。
function pad(s, n) {
var v = '',
i;
for(i = 0; i < n - s.length; i += 1) {
v += '0';
}
return v + s;
}
var sorted = arr.sort(function (a, b) {
var idDiff = a.id - b.id;
if (idDiff) {
return idDiff;
}
var ordA = a.date.split('-').reverse(),
ordB = b.date.split('-').reverse();
ordA[0] = pad(ordA[0], 6);
ordA[1] = pad(ordA[1], 2);
ordA[2] = pad(ordA[2], 2);
ordA = ordA.join('');
ordB[0] = pad(ordB[0], 6);
ordB[1] = pad(ordB[1], 2);
ordB[2] = pad(ordB[2], 2);
ordB = ordB.join('');
return ordA - ordB;
});
上
如果你真的想使用Date
个对象,我建议如下。
var sorted = arr.sort(function (a, b) {
var idDiff = a.id - b.id;
if (idDiff) {
return idDiff;
}
var ordA = a.date.split('-').reverse(),
ordB = b.date.split('-').reverse();
ordA[1] -= 1;
ordB[1] -= 1;
return new Date(Date.UTC.apply(undefined, ordA)).valueOf() - new Date(Date.UTC.apply(undefined, ordB)).valueOf();
});
sorted.forEach(function (element) {
console.log(JSON.stringify(element));
});
上
注意:这些示例不处理负数年份的日期,您需要进一步修改。
答案 1 :(得分:5)
首先与id进行比较,然后如果id相等则与date进行比较。但由于您的日期格式无效,因此必须进行额外的工作才能让Date
识别出来。
sorted_arr = arr.sort(function(a, b) {
return a.id - b.id || new Date(a.date.split('-').reverse().join('-')) - new Date(b.date.split('-').reverse().join('-'));
});
编辑: 如果您保证在1位数月份和日期之前有零,那么您甚至可以不解析到目前为止:
sorted_arr = arr.sort(function(a, b) {
return a.id - b.id || a.date.split('-').reverse().join('') - b.date.split('-').reverse().join('');
});
答案 2 :(得分:1)
最好让日期操作lib momentjs来帮助。 您可以查看jsBin
上的代码function compare(a, b){
var idDiff = a.id - b.id;
var adate = moment(a.date, "DD-MM-YYYY");
var bdate = moment(b.date, "DD-MM-YYYY");
var dateDiff = adate.diff(bdate);
return idDiff || dateDiff;
}
var sortedArr = arr.sort(compare);
console.log(sortedArr);
答案 3 :(得分:1)
您可以使用Alasql库按两个属性对数组进行排序:
var res = alasql('SELECT *, mid(date,7,4)+mid(date,4,2)+mid(date,1,2) AS ndate \
FROM ? ORDER BY id, ndate',[arr]);
试试这个例子at jsFiddle。
此处&#34; mid(日期,7,4)+ mid(日期,4,2)+ mid(日期,1,2)&#34;用于转换日期来自&#39; 28-11-2014&#39;到排序键,如&#39; 20141128&#39;。
答案 4 :(得分:0)