I have problem in sorting an array of objects
The object looks like
All I want is to make the *ngFor loop sort it by 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 tried to implement this code but it doesn't seem to working. What is wrong in my code or is there anything that needs to be done?