I am facing difficulties in sorting an array of objects
The structure of the object is as follows:
https://i.sstatic.net/z5UMv.png
My goal is to sort the *ngFor loop based on the group_id property.
component.html
<ul *ngFor="let list of selectgid | groupid">
<li>{{list}}</li>
</ul>
pipe.ts
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'groupid'
})
export class GroupidPipe implements PipeTransform {
transform(array: Array<any>): Array<any> {
if (array !== undefined) {
array.sort((a: any, b: any) => {
if (a.group_id < b.group_id) {
return -1;
} else if (a.group_id > b.group_id) {
return 1;
} else {
return 0;
}
});
}
return array;
}
}
I have attempted to implement this code but it seems to be not working. Can anyone point out what might be wrong with my code or suggest any additional steps that need to be taken?