VadimDez / ngx-order-pipe

▼ Angular 5+ orderBy pipe
https://vadimdez.github.io/ngx-order-pipe/
MIT License
243 stars 57 forks source link

how to use comparator? #39

Closed dgpoo closed 6 years ago

dgpoo commented 6 years ago

i have opened issue: https://github.com/VadimDez/ngx-order-pipe/issues/34 and given solution to use comparator so how can use it?can you give me real time example? Thanx in advance

VadimDez commented 6 years ago

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 orderBys last argument:

<tr *ngFor="let row of collection | orderBy: 'name':false:'case-insensitive':customComparator">
...
dgpoo commented 6 years ago

okay @VadimDez Thank you

ghost commented 6 years ago

@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"

VadimDez commented 6 years ago

@AdrianNg could you provide collection?

ghost commented 6 years ago

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