Advertisement
Guest User

Tickets

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