rdt-client/client/src/app/sort.pipe.ts
2025-02-28 12:28:21 +00:00

23 lines
569 B
TypeScript

import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'sort',
standalone: false
})
export class SortPipe implements PipeTransform {
transform(array: any[], field: string, order: 'asc' | 'desc' = 'asc'): any[] {
if (!Array.isArray(array)) {
return [];
}
const sortedArray = array.sort((a, b) => {
if (a[field] < b[field]) {
return -1;
} else if (a[field] > b[field]) {
return 1;
} else {
return 0;
}
});
return order === 'asc' ? sortedArray : sortedArray.reverse();
}
}