我想按其、姓氏、和每个字母表下的名字列表对联系人列表进行排序。它可以很容易地在jquery和角-1中完成,但是如何在angular 2/Ionic V2中实现逻辑呢?
发布于 2017-04-10 17:56:34
创建一个管道,并对所需的值进行排序。
见此示例:
import { Pipe } from "angular2/core";
import {Todo} from './todo';
@Pipe({
name: "sort"
})
export class TodosSortPipe {
transform(array: Array<Todo>, args: string): Array<Todo> {
// Do YOUR LOGIC HERE, like this code below
array.sort((a: any, b: any) => {
if (a < b) {
return -1;
} else if (a > b) {
return 1;
} else {
return 0;
}
});
return array;
}
}然后用它就像:
*ngFor="let item of array | sort"来源:
官方医生:
发布于 2017-04-10 21:28:59
所以,您可以这样做,我已经在我的应用程序中组织了联系人列表。在代码"data“中是联系人列表。然后,"a.last_name“是根据如何设置联系人列表对象来访问姓氏类别的。希望它能有所帮助,这将使数据对象在排序之后。
data.sort(function (a, b) {
return a.last_name.localeCompare(b.last_name);
})https://stackoverflow.com/questions/43328854
复制相似问题