maymunskoa

08. Tickets /Objects and Classes/

Jul 6th, 2020
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solve(data, sorting){
  2.  
  3.     class Ticket{
  4.         constructor(destination, price, status){
  5.             this.destination = destination;
  6.             this.price = price;
  7.             this.status = status;
  8.         }
  9.     }
  10.  
  11.  
  12.     const result = [];
  13.     for (const line of data) {
  14.         const [destination, price, status] = line.split('|');
  15.         result.push(new Ticket(destination, Number(price), status));
  16.     }
  17.     result.sort((a,b)=>{
  18.         if(sorting === 'destination'){
  19.             return a.destination.localeCompare(b.destination);
  20.         }else if(sorting === 'price'){
  21.             return a.price - b.price;
  22.         }
  23.         return a.status.localeCompare(b.status);
  24.     });
  25.     return result;
  26. }
Advertisement
Add Comment
Please, Sign In to add comment