I am struggling to organize an observable that is retrieved from a service using http.get. I have attempted to sort the data both from the service and in the component after subscribing. However, I keep encountering the error "Sort is not a function". As a newcomer to Angular, I am having trouble with sorting. Any assistance would be greatly appreciated. Thank you.
JSON sample
"data": [
{
"codeValue": 2,
"codeType": 145,
"description": "ASSIMILATION"
},
{
"codeValue": 3,
"codeType": 145,
"description": "BANKRUPTCY"
},
{
"codeValue": 4,
"codeType": 145,
"description": "BID TENDER"
},
event.interface.ts
export interface IEventInterface {
codeValue: number;
codeType: number;
description: string;
}
Service.ts
getEventType():
{
return this.http.get<IEventInterface[]>('/gpscaservices/v1/system-
descriptions?codeType=145&codeValue=');
}
Create.Component.ts
orderedEvent: any;
eventTypes: any;
this._dataService.getEventType().subscribe({
next: (response: IEventInterface[]) => {
this.eventTypes = response ;
this.sortBy('description');
}
sortBy(field: string ) {
this.eventTypes.sort((a: any, b: any) =>{
if (a[field] < b[field]) {
return -1;
} else if (a[field] > b[field]) {
return 1;
} else {
return 0;
}
});
this.orderedEvent = this.eventTypes;
Create.Component.html
nx-option *ngFor="let eventType of orderedEvent.data"
[value]="eventType.codeValue">
{{ eventType.description }}
/nx-option>