Advertisement
AreWe

Tickets

Jul 2nd, 2020
1,127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. tickets = (data = [], criteria = '') => {
  2.     class Ticket {
  3.         constructor(destination, price, status) {
  4.             this.destination = destination;
  5.             this.price = Number(price);
  6.             this.status = status;
  7.         }
  8.     }
  9.  
  10.     const statuses = {
  11.         'available': 1,
  12.         'departed': 2,
  13.         'sold': 3
  14.     };
  15.  
  16.     const sortAlpabetically = (str1, str2) => {
  17.         return str1.localeCompare(str2);
  18.     };
  19.  
  20.     const sortTickets = (a, b) => {
  21.         if(criteria === 'price') {
  22.             return a.price - b.price;
  23.         } else if(criteria === 'status') {
  24.             return statuses[a.status] - statuses[b.status];
  25.         }
  26.  
  27.         return sortAlpabetically(a.destination, b.destination);
  28.     };
  29.  
  30.     return data
  31.         .map(str => new Ticket(...str.split('|')))
  32.         .sort(sortTickets || sortAlpabetically(a.destination, b.destination));
  33.  
  34. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement