sorting the http get before returning to component

Viewed 75

Currently in the service i am calling http.get as follow

getList(): Observable<Customer[]> {
return this.http.get<Customer[]>("api/customers");

}

in component ngOnInit the list of customer is returned:

this.customerService.getList().subscribe(data => (this.customers = data));

It is working fine. Now i want to sorting the list before returning to the component.

any idea of how to return sorted list in the service to compnent?

1 Answers

You can sort it in the service by calling .sort,

this.http.get<Customer[]>("api/customers").pipe(
  map(results => results.sort(...))
);
Related