In Angular, how can I create pipe and apply sort.
I don't know how to create. Can any one give me any guidance on this?
<div *ngFor="let item of data | keyvalue">
<div *ngFor='let obj of item.value; index as i'>
{{ obj.curdate }}
{{ obj.time }}
</div>
</div>
I saw a lot of answers on this subject.
But I don't know where to create this file and how to call in my component.
Can suggest step by step tutorial on this.
I tried this,
pipe/orderBy.pipe.ts
import { Pipe, PipeTransform } from "@angular/core";
@Pipe({
name: "sort"
})
export class ArraySortPipe implements PipeTransform {
transform(array: any, field: string): any[] {
if (!Array.isArray(array)) {
return;
}
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;
}
}
in app.module.ts
import { ArraySortPipe } from './pipe/orderBy.pipe';
@NgModule({
declarations: [
ArraySortPipe
],
mycomponent.ts
import { ArraySortPipe } from '../../../pipe/orderBy.pipe';
mytemplate
<th *ngFor="let obj of item.value; index as i | sort: 'obj.curdate'">{{obj.curdate}}</th>
I followed above method but i got an error.
compiler.js:2547 Uncaught Error: Template parse errors: TypeError: Cannot read property 'toUpperCase' of undefined (" ]*ngFor="let obj of item.value; index as i | sort: 'obj.curdate'">{{obj.curdate}}