0

I'm fetched movies reviews from NYT api and I want to sort result by date in descending order, How can I add this filter ?

service

import { HttpClient, HttpParams } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable, throwError } from 'rxjs';
import { catchError, retry } from 'rxjs/operators';
import { GlobalConstants } from '../common/global-constants';
import {map} from 'rxjs/operators';
@Injectable()
export class ReviewsService {
  apiUrl = GlobalConstants.apiURL;

  constructor(private http: HttpClient) {
  }

  getReviews(): Observable<any> {
    return this.http.get(this.apiUrl).pipe(map((response:any)=>response.results))
  }
}

component

import { Component, OnInit } from '@angular/core';
import { ReviewsService } from 'src/app/services/reviews.service';
@Component({
  selector: 'app-reviews',
  templateUrl: './reviews.component.html',
  styleUrls: ['./reviews.component.css']
})
export class ReviewsComponent implements OnInit {

  data: any;
  constructor(private reviewsService: ReviewsService) { }

  ngOnInit() {
    this.getReviews();
  }

  getReviews() {
    this.reviewsService.getReviews().subscribe({
      next: (data: any) => {
        this.data = data;  
        console.log(typeof this.data);
        console.log(this.data);
        
              
      },
      error: (err: any) => {
        console.log(err);
      },
      complete: () => {
        console.log('complete');
      }
    });
  }

}

1 Answer 1

1

in your service:

     getReviews(): Observable<any> {
       return this.http.get(this.apiUrl).pipe(map((response: any) 
       => response.results.sort((dataA, dataB) => {
       return ((+new Date(dataA['publication_date'])) - (+new 
       Date(dataB['publication_date'])));
     })))
    }
Sign up to request clarification or add additional context in comments.

1 Comment

make sense, thanks Bellarej

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.