import { Injectable } from '@angular/core';
import { Writer } from '../models/writer';

@Injectable({
  providedIn: 'root'
})
export class WriterService {

  constructor() { }

  getAllWriters(): Writer[] {
    return [
      {firstname: "Pera", lastname: "Peric", awards: 3,
       birthday: new Date('1995-12-1')},

      {firstname: "Zika", lastname: "Peric", awards: 5,
       birthday: new Date('1993-12-12')}
    ]
  }

  sortByAwards(): Writer[]{
    return this.getAllWriters().sort((w1,w2)=>{
      return w2.awards - w1.awards
    })
  }

  sortByBirth(): Writer[]{
    return this.getAllWriters().sort((w1,w2)=>{
      if(w1.birthday<w2.birthday){
        return -1
      }
      else{
        if(w1.birthday==w2.birthday){
          return 0
        }
        else return 1
      }
    })
  }
}
