我有以下数组:
let datas = ["nov/2018", "set/2018", "jan/2019", "dez/2018", "out/2018"]
我执行时
datas.sort()
他按字母顺序排序,但是,我必须先按年份排序,然后再按字母顺序排序。
["dez/2018", "jan/2019", "nov/2018", "out/2018", "set/2018"]
今天测试,我到达了以下代码行:
var datas = ["mar", "abr", "jan", "dez", "set", "mai", "jun", "out", "jul"];
var datas_corretas = ["jan", "fev", "mar", "abr", "mai", "jun", "jul", "ago", "set", "out", "nov", "dez"];
var result = [];
datas_corretas.forEach(function ordenar(element, index){
var mes = datas.filter(function(valor){
return valor == datas_corretas[index];
});
result.push(mes[0]);
for(element of result){
if (element === undefined || element === null){
result.pop(element);
}
}
});
console.log(result);
那样我可以对数据进行排序,但是问题是当我将其与年份一起使用时
var datas = ["mar/2018", "abr/2018", "jan/2019", "dez/2018", "set/2018", "mai/2018", "jun/2018", "out/2018", "jul/2018"];
有人知道我该如何解决吗?
答案 0 :(得分:1)
您可以分割日期,然后先按年份排序,然后再按月份索引排序。
var months = ["jan", "fev", "mar", "abr", "mai", "jun", "jul", "ago", "set", "out", "nov", "dez"],
datas = ["nov/2018", "set/2018", "jan/2019", "dez/2018", "out/2018"];
datas.sort((a, b) => {
var aa = a.split('/'),
bb = b.split('/');
return aa[1] - bb[1] || months.indexOf(aa[0]) - months.indexOf(bb[0]);
});
console.log(datas);
为了更快地访问,您可以将月份存储为对象。
var months = { jan: 1, fev: 2, mar: 3, abr: 4, mai: 5, jun: 6, jul:7, ago: 8, set: 9, out: 10, nov: 11, dez: 12 },
datas = ["nov/2018", "set/2018", "jan/2019", "dez/2018", "out/2018"];
datas.sort((a, b) => {
var aa = a.split('/'),
bb = b.split('/');
return aa[1] - bb[1] || months[aa[0]] - months[bb[0]];
});
console.log(datas);
答案 1 :(得分:0)
但是,我必须先按年份排序,然后再按字母顺序排序 订单。
["dez/2018", "jan/2019", "nov/2018", "out/2018", "set/2018"]
要获得预期结果,您可以使用两次.sort()
,分别在.split()
,"/"
和.pop()
上使用.shift()
datas.sort((a, b) => a.split("/").pop() < b.split("/").pop() ? -1 : 0)
.sort((a, b) => a.split("/").shift() < b.split("/").shift() ? -1 : 0);
答案 2 :(得分:0)
直到您可以将自定义比较器传递给javascript的sort
函数。通过首先比较年份,然后比较月份,我们可以使用它来编写自己的排序逻辑:
let datas = ["nov/2018", "set/2018", "jan/2019", "dez/2018", "out/2018"]
const orderedMonths = ["jan", "fev", "mar", "abr", "mai", "jun", "jul", "ago", "set", "out", "nov", "dez"];
datas.sort((a,b) => {
var [montha,yeara] = a.split('/');
var [monthb,yearb] = b.split('/');
if (yeara > yearb) return 1;
if (yearb > yeara) return -1;
if (orderedMonths.indexOf(montha) > orderedMonths.indexOf(monthb)) return 1;
if (orderedMonths.indexOf(monthb) > orderedMonths.indexOf(montha)) return -1;
return 0;
});
console.log(datas);
答案 3 :(得分:0)
将值转换为数字和索引时非常容易。
下面是我的示例。
var datas_corretas = ["jan", "fev", "mar", "abr", "mai", "jun", "jul", "ago", "set", "out", "nov", "dez"];
var datas = ["mar/2018", "abr/2018", "jan/2019", "dez/2018", "set/2018", "mai/2018", "jun/2018", "out/2018", "jul/2018"];
var result = datas.sort(function(a, b){
// index of the month
var aMonth = datas_corretas.indexOf(a.split("/")[0]);
// convert the year to number
var aYear = parseInt(a.split("/")[1]);
var bMonth = datas_corretas.indexOf(b.split("/")[0])
var bYear = parseInt(b.split("/")[1])
return aYear - bYear || aMonth - bMonth;
});
console.log(result)
答案 4 :(得分:0)
I must order first by the year and then by alphabetical order.
Answers seem to all sort the month chronologically, so here's how to sort them alphabetically. Split in to month and year, then firstly compare years then compare months using localeCompare.
let datas = ["nov/2018", "set/2018", "jan/2019", "dez/2018", "out/2018"]
datas.sort((a, b) => {
let [amon, ayr] = a.split('/');
let [bmon, byr] = b.split('/');
return (ayr - byr) || amon.localeCompare(bmon);
});
console.log(datas);