angular - Order Json object -




i'm creating simple movie listing app angular 4. i'm making http request fetch movies stored in json file. have fields "id", "title", "genre", "duration", etc. when i'm listing movies, how can order them id descending, last 1 appear first?

here's code using json data:

on data service file:

getmovies(){       return this.http.get('assets/data/movies.json')       .map(res => res.json());    } 

on component.ts file:

export class maincomponent implements oninit {   movies: movies[];   username:string;   userimg:string;    constructor(private userservice:userservice, private dataservice:dataservice) { }    ngoninit() {      this.dataservice.getmovies().subscribe((movies) =>{         this.movies = movies;     });     }   }  interface movies {     id:number,     title:string,     year:number,     rating:number,     cover:string,     genre:string,     duration:string,     description:string,     favourite:number } 

on component.html file:

<div *ngfor="let movie of movies" class="row row-movies">               <a [routerlink]="['/movies', {'id': movie.id}]">                 <div class="col-md-9">                     <h3> {{movie.title}}</h3>                     <h4> {{movie.year}}</h4>                     <h4> {{movie.rating}}</h4>                     <p>{{movie.description}}</p>                     <h5> {{movie.genre}}</h5>                     <h5> {{movie.duration}}</h5>                 </div>                 <div class="col-md-3">                   <img src="../assets/img/capas/movies/{{movie.capa}}" class="img-responsive capa-filme" width="350px" />                 </div>               </a>       </div> 

can me please? i'm still noob angular..

this question has nothing angular. need do, after loading movies, this:

this.movies.sort((a,b) => (b.id - a.id)); 

as note: works using array in vanilla javascript.

https://developer.mozilla.org/en-us/docs/web/javascript/reference/global_objects/array/sort





wiki

Comments

Popular posts from this blog

Asterisk AGI Python Script to Dialplan does not work -

python - Read npy file directly from S3 StreamingBody -

kotlin - Out-projected type in generic interface prohibits the use of metod with generic parameter -