Sort array by the date

I have array from json like this

0: {release_date: “2018-02-13”, id: 301337, video: false, vote_average: 5.1, title: “Polution”}
1: {release_date: “2018-02-13”, id: 452773, video: false, vote_average: 6.4, title: "Tad "}
2: {release_date: “2018-02-13”, id: 389015, video: false, vote_average: 7.3, title: “I,” }

I use this service to call them, and sort them using "sort_by","release_date.desc". But it’s not working at all.

getUpcoming(){
let params: URLSearchParams = new URLSearchParams();
let headers = new Headers();
headers.append(‘Content-Type’, ‘application/vnd.api+json’);
headers.append(‘Accept’, ‘application/vnd.api+json’);

params.set("api_key","4285b6347f0429f60f6364ca644beab2");
params.set("sort_by","release_date.desc");

let options = new RequestOptions({
  headers: headers,
  search: params
});

return this.http.get(this.API+"movie/upcoming", options)
  .map(
  (res: Response) => {
    return res.json()
  })

}

How do I sort the array by the date? Thanks before…

You can do it in your ionic code:

return this.http.get(this.API+"movie/upcoming", options)
  .map((res: Response) => res.json())
  .map((data: Array<{release_date: string, id: number, video: boolean, vote_average: number, title: string}>) =>
    data.sort((a, b) => a.release_date <= b.release_date ? -1 : 1));
2 Likes

thanks for your help! :smile: