【发布时间】:2016-08-26 22:23:54
【问题描述】:
如何在 Typescript 中按多个字段对数组列表进行排序。
例如,我有这个对象: 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;
}
}
但它不起作用。
【问题讨论】:
-
你到底是怎么排序的?你在排序什么?你在这里展示的只是比较函数,但你用它做什么呢?另外,您能否详细说明什么不起作用?
-
我有一个对象“RSFolderObject”。您可以在链接中看到上面的图像。这就是我要排序的对象。 1) var myObject: RSFolderObject[] = []; 2).....比将现有对象推送到 myObject..... 3) myObject.sort(this.sortFunction);
标签: arrays list sorting typescript