Advertisement
Guest User

SoftUni, Tickets, JS

a guest
Oct 3rd, 2020
405
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function test(input, sort) {
  2.     class Ticket {
  3.         constructor(destination, price, status) {
  4.             this.destination = destination;
  5.             this.price = Number(price);
  6.             this.status = status;
  7.         }
  8.     }
  9.     let arr = [];
  10.     for (const row of input) {
  11.         let [destination, price, availability] = row.split('|');
  12.         arr.push(new Ticket(destination, price, availability));
  13.     }
  14.     switch (sort) {
  15.         case "destination":
  16.             arr.sort(function(a, b) {
  17.               if (a.destination < b.destination) {
  18.                 return -1;
  19.               }
  20.               if (a.destination > b.destination) {
  21.                 return 1;
  22.               }
  23.               return 0;}
  24.             );
  25.  
  26.             break;
  27.         case "price":
  28.             arr.sort();
  29.             break;
  30.         case "status":
  31.             arr.sort(function(a, b) {
  32.               if (a.status < b.status) {
  33.                 return -1;
  34.               }
  35.               if (a.status > b.status) {
  36.                 return 1;
  37.               }
  38.               return 0;}
  39.             );
  40.  
  41.             break;
  42.         default:
  43.             break;
  44.     }
  45.     return arr;
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement