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

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

  constructor() { }

  getAllWriters(): Writer[]{
    return [{firstname: "Petar", lastname: "Petrovic", awards: 5, birthday: new Date("1996-12-12")},
      {firstname: "Mirko", lastname: "Mirkovic", awards: 1, birthday: new Date("1994-12-12")},
      {firstname: "Zika", lastname: "Lazovic", awards: 3, birthday: new Date("1998-12-12")}
    ]
  }

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

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