I am facing a challenge where I require each column of a table to be sorted in ascending order every time it is clicked. The sorting logic implemented is a standard JavaScript method. While this method works well in most scenarios, it encounters issues when the data contains numbers with varying digits.
Below is the code snippet:
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 implementation fails to correctly sort values like 844401, 76574893632, 717613, and 6304420005555.
Instead of sorting them in descending order of magnitude as expected (76574893632 before 844401), it arranges them differently.