我想按主题 (字符串,字母)排序数组,然后级别 (int,升序) ,然后名称 (字符串,字母)。我在SO上发现了许多其他线程,但我没有找到任何由多个变量类型排序的线程。我以为我拥有它,但它没有正确排序。
排序功能:
scipads.sort(function (a, b) {
if (a.subject != b.subject)
return a.subject < b.subject;
if (a.level != b.level)
return a.level - b.level;
return a.name < b.name;
});
数组:
var scipads = [{
"name": "L2 Physics Externals",
"level": 2,
"subject": "physics",
}, {
"name": "L2 Physics Internals",
"level": 2,
"subject": "physics",
}, {
"name": "L2 Chem Externals",
"level": 2,
"subject": "chemistry",
}, {
"name": "L2 Chem Internals",
"level": 2,
"subject": "chemistry",
}, {
"name": "L2 Bio Internals",
"level": 2,
"subject": "biology",
}, {
"name": "L2 Bio Externals",
"level": 2,
"subject": "biology",
}, {
"name": "L1 Electricity & Magnetism",
"level": 1,
"subject": "physics",
}, {
"name": "L1 Wave Behaviour",
"level": 1,
"subject": "physics",
}, {
"name": "L1 Heat",
"level": 1,
"subject": "physics",
}, {
"name": "L1 Carbon Chemistry",
"level": 1,
"subject": "chemistry",
}, {
"name": "L1 Selected Elements",
"level": 1,
"subject": "chemistry",
}, {
"name": "L1 Chemical Reactions",
"level": 1,
"subject": "chemistry",
},
];
我如何按主题排序,然后按级别排序,然后命名?
答案 0 :(得分:2)
比较应return an integer。这是一个例子:
scipads.sort((a, b) => {
if (a.subject != b.subject) {
return a.subject < b.subject ? -1 : 1;
}
if (a.level != b.level) {
return a.level - b.level;
}
return a.name < b.name ? -1 : 1;
})
答案 1 :(得分:1)
您的排序比较功能已关闭,但尚未完全开启。你应该返回-1表示小于0,0表示相等,1表示大于。有关文档,请参阅here。
示例:将return a.subject < b.subject
更改为return a.subject < b.subject ? -1 : 1;
答案 2 :(得分:1)
使用localeCompare
比较字符串
var scipads=[{name:"L2 Physics Externals",level:2,subject:"physics"},{name:"L2 Physics Internals",level:2,subject:"physics"},{name:"L2 Chem Externals",level:2,subject:"chemistry"},{name:"L2 Chem Internals",level:2,subject:"chemistry"},{name:"L2 Bio Internals",level:2,subject:"biology"},{name:"L2 Bio Externals",level:2,subject:"biology"},{name:"L1 Electricity & Magnetism",level:1,subject:"physics"},{name:"L1 Wave Behaviour",level:1,subject:"physics"},{name:"L1 Heat",level:1,subject:"physics"},{name:"L1 Carbon Chemistry",level:1,subject:"chemistry"},{name:"L1 Selected Elements",level:1,subject:"chemistry"},{name:"L1 Chemical Reactions",level:1,subject:"chemistry"}];
scipads.sort(function(a, b) {
if (a.subject != b.subject)
return a.subject.localeCompare(b.subject);
else if (a.level != b.level)
return a.level - b.level;
return a.name.localeCompare(b.name);
});
console.log(scipads);
&#13;
答案 3 :(得分:0)
我的解决方案,首先比较主题,然后比较水平
var scipads=[{name:"L2 Physics Externals",level:2,subject:"physics"},{name:"L2 Physics Internals",level:2,subject:"physics"},{name:"L2 Chem Externals",level:2,subject:"chemistry"},{name:"L2 Chem Internals",level:2,subject:"chemistry"},{name:"L2 Bio Internals",level:2,subject:"biology"},{name:"L2 Bio Externals",level:2,subject:"biology"},{name:"L1 Electricity & Magnetism",level:1,subject:"physics"},{name:"L1 Wave Behaviour",level:1,subject:"physics"},{name:"L1 Heat",level:1,subject:"physics"},{name:"L1 Carbon Chemistry",level:1,subject:"chemistry"},{name:"L1 Selected Elements",level:1,subject:"chemistry"},{name:"L1 Chemical Reactions",level:1,subject:"chemistry"}];
// sort by subject, then level, then name
var res = scipads.sort((a, b) => {
if (a.subject > b.subject) {
return 1;
} else if (a.subject < b.subject) {
return -1;
} else if ( a.level > b.level){
return 1;
} else if (a.level < b.level) {
return -1;
} else if (a.name > b.name) {
return 1;
} else if (a.name < b.name) {
return -1;
}
return 0;
});
console.log(res)
&#13;
答案 4 :(得分:0)
要比较字符串,您应该使用string.localeCompare
。此外,您可以尝试创建一个具有优先级列表的数组,并在其上循环进行排序。
var scipads=[{name:"L2 Physics Externals",level:2,subject:"physics"},{name:"L2 Physics Internals",level:2,subject:"physics"},{name:"L2 Chem Externals",level:2,subject:"chemistry"},{name:"L2 Chem Internals",level:2,subject:"chemistry"},{name:"L2 Bio Internals",level:2,subject:"biology"},{name:"L2 Bio Externals",level:2,subject:"biology"},{name:"L1 Electricity & Magnetism",level:1,subject:"physics"},{name:"L1 Wave Behaviour",level:1,subject:"physics"},{name:"L1 Heat",level:1,subject:"physics"},{name:"L1 Carbon Chemistry",level:1,subject:"chemistry"},{name:"L1 Selected Elements",level:1,subject:"chemistry"},{name:"L1 Chemical Reactions",level:1,subject:"chemistry"}];
var priority = ["subject", "level", "name"]
scipads.sort(function(a,b){
var val = 0;
priority.some(function(k){
val = compare(a,b,k);
return val !== 0;
});
})
function compare(a,b,k){
switch(typeof a[k]){
case "string": return a[k].localeCompare(b[k]);
case "number": return a[k] - b[k]
}
}
console.log(scipads)