Ivankooo1

Tickets

Aug 30th, 2020
2,717
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(tickets,criteria){
  2.  
  3. //    const comparator = {
  4. //        destination: (a,b) => (a.destination.localeCompare(b.destination)),
  5. //        price: (a,b) => (a - b),
  6. //        status: (a,b) => (a.status.localeCompare(b.status))
  7. //    }
  8.  
  9.    class Ticket {
  10.        constructor(descriptor){
  11.            const [destination,price,status] = descriptor.split("|");
  12.            this.destination = destination,
  13.            this.price = +price,
  14.            this.status = status
  15.        }
  16.    }
  17.    return tickets.map(t=> new Ticket(t)).sort(comparator[criteria]);
  18.  
  19.    function comparator(a,b){
  20.       try{
  21.         return a[criteria].localeCompare(b[criteria])
  22.       }catch{
  23.         return a[criteria] - b[criteria];
  24.       }
  25.    }
  26.  
  27. }
  28. console.log(solve(
  29. // ['Philadelphia|94.20|available',
  30. // 'New York City|95.99|available',
  31. // 'New York City|95.99|sold',
  32. // 'Boston|126.20|departed'],
  33. // 'destination'
  34. ['Philadelphia|94.20|available',
  35.  'New York City|95.99|available',
  36.  'New York City|95.99|sold',
  37.  'Boston|126.20|departed'],
  38. 'status'
  39.  
  40. ));
Advertisement
Add Comment
Please, Sign In to add comment