I am looping through all the posts
<li *ngFor="let post of posts">
When displaying the date for each post I do:
{{post.date | date:'yyyy-MM-dd HH:mm:ss'}}
What I want to do is display all the posts in order of newest first.
I have tried using a pipe like:
<li *ngFor="let post of posts | order-by-pipe">
import {Pipe, PipeTransform} from '@angular/core';
@Pipe({
name: 'order-by-pipe'
})
export class OrderByPipe implements PipeTransform{
transform(array: Array<string>, args: string): Array<string> {
if(!array || array === undefined || array.length === 0) return null;
array.sort((a: any, b: any) => {
if (a.date < b.date) {
return -1;
} else if (a.date > b.date) {
return 1;
} else {
return 0;
}
});
return array;
}
}
But it does not work. I get the error:
TypeError: Cannot read property 'toUpperCase' of undefined ("
[ERROR ->]*ngFor="let post of posts | order-by-pipe">
Any help would be welcome, thanks