I have a custom pipe that orders an array of objects by a prop that is of type number.
Template where pipe is being used
<div *ngFor="let product of products | orderBy:'price'">
OrderByPipe
export class OrderByPipe implements PipeTransform {
transform(array: any[], field: string): any[] {
array.sort((a: any, b: any) => {
if (a[field] < b[field]) {
return -1;
} else if (a[field] > b[field]) {
return 1;
} else {
return 0;
}
});
return array;
}
}
The pipe seems to work for smaller arrays and when I use forEach to loop through each item. However when I console the array at the end and when it is returned in the template, the array is still out of order.
I am not entirely sure what might be causing this, thanks.