how to use comparator?
dgpoo opened this issue · 5 comments
i have opened issue: #34
and given solution to use comparator so how can use it?can you give me real time example?
Thanx in advance
Create a method in you component's class (for example customComparator
):
customComparator(itemA, itemB) {
return itemA > itemB ? 1 : -1;
}
then in your template add customComparator
as orderBy
s last argument:
<tr *ngFor="let row of collection | orderBy: 'name':false:'case-insensitive':customComparator">
...
@VadimDez Hi, my collection is from AJAX response <tr *ngFor="let row of collection | orderBy: 'name':false:'case-insensitive':customComparator">, and when I want to use this (https://stackoverflow.com/questions/4340227/sort-mixed-alpha-numeric-array) function as my customComparator , it gave me "Cannot read property 'replace' of undefined"
Hi @VadimDez , this is the JSON collection returned from AJAX in which i want to sort by the "bid" field.
But your sorting algorithm sorts it like this :
[{bid: "0 abc", gid: "Group1", gn: "Group1", status: "A"} , {bid: "10 abc", gid: "Group1", gn: "Group1", status: "A"} , {bid: "2 abc", gid: "Group1", gn: "Group1", status: "A"} , {bid: "3 abc", gid: "Group1", gn: "Group1", status: "A"} ]
However , what i want is this below so i need a customComparator :
[{bid: "0 abc", gid: "Group1", gn: "Group1", status: "A"} , {bid: "2 abc", gid: "Group1", gn: "Group1", status: "A"} , {bid: "3 abc", gid: "Group1", gn: "Group1", status: "A"} , {bid: "10 abc", gid: "Group1", gn: "Group1", status: "A"} ]
Thank you very much,
Adrian