Advertisement
kstoyanov

08. Tickets

Sep 22nd, 2020
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function tickets(arr = [], 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 result = [];
  11.  
  12.   arr.forEach((line) => {
  13.     const [destination, price, status] = line.split('|');
  14.     const data = new Ticket(destination, price, status);
  15.     result.push(data);
  16.   });
  17.  
  18.   switch (criteria) {
  19.     case 'destination':
  20.       result.sort((a, b) => a.destination.localeCompare(b.destination));
  21.       break;
  22.     case 'price':
  23.       result.sort((a, b) => a.price - b.price);
  24.       break;
  25.     case 'status':
  26.       result.sort((a, b) => a.status.localeCompare(b.status));
  27.       break;
  28.     default:
  29.       break;
  30.   }
  31.  
  32.   return result;
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement