如何通过多个字段对Typescript中的arraylist进行排序。
例如,我有这个对象: enter image description here
我的单一排序方法正常工作:
private sortFunction(a: RSFolderObject, b: RSFolderObject) {
var a_label = a.label.toLowerCase();
var b_label = b.label.toLowerCase();
if (a_label < b_label) {
return -1;
} else if (a_label > b_label) {
return 1;
} else {
return 0;
}
}
对于多次排序我使用了:
private sortFunction(a: RSFolderObject, b: RSFolderObject) {
var a_label = a.label.toLowerCase();
var b_label = b.label.toLowerCase();
var a_description = a.description[0].toLowerCase().replace("\\", "");
var b_description = b.description[0].toLowerCase().replace("\\", "");
if (a_label < b_label || a_description < b_description) {
return -1;
} else if (a_label > b_label || a_description > b_description) {
return 1;
} else {
return 0;
}
}
但它不起作用。
答案 0 :(得分:1)
private sortFunction(a: RSFolderObject, b: RSFolderObject) {
var a_label = a.label.toLowerCase();
var b_label = b.label.toLowerCase();
var a_description = a.description[0].toLowerCase().replace("\\", "");
var b_description = b.description[0].toLowerCase().replace("\\", "");
if (a_label < b_label) {
return -1;
}
else if (a_label > b_label) {
return 1;
}
else {
if (a_description < b_description) {
return -1;
}
else if (a_description > b_description) {
return 1;
}
else {
return 0;
}
}
}