I have a requirement where I need to sort each column of table every time it is clicked in ascending order. The logic applied is a general logic for sorting in Javascript. It works fine in all the scenarios except when the data comes up with different digits the column.
The code is
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'orderBy'
})
export class OrderByPipe implements PipeTransform {
transform(records: Array<any>, args?: any): any {
return records.sort(function(a, b){
if(a[args.property] === null){
return 1 * args.direction;
}
else if(b[args.property] === null){
return -1 * args.direction;
}
else if(a[args.property] < b[args.property]){
return -1 * args.direction;
}
else if( a[args.property] > b[args.property]){
return 1 * args.direction;
}
else{
return 0;
}
});
};
}
the above code fails when I get data like 844401, 76574893632,717613, 6304420005555
It sorts the values in the order listed above although it should sort 76574893632 before 844401