Advertisement
YavorJS

2. Tickets

Jul 10th, 2017
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. function main(arr, dest) {
  2.  
  3. class Ticket {
  4. constructor(destination,price,status) {
  5. this.destination=destination;
  6. this.price=Number(price);
  7. this.status=status;
  8. }
  9. }
  10.  
  11. let tickets=[];
  12.  
  13. for (let obj of arr) {
  14. let [destination,price,status]=obj.split('|');
  15. let ticket= new Ticket(destination,price,status);
  16. tickets.push(ticket);
  17. }
  18.  
  19. //Sorting
  20.  
  21. function compare(a, b) {
  22. const ticketA = a[dest];
  23. const ticketB = b[dest];
  24.  
  25. let comparison = 0;
  26. if (ticketA > ticketB) {
  27. comparison = 1;
  28. } else if (ticketA < ticketB) {
  29. comparison = -1;
  30. }
  31. return comparison;
  32. }
  33.  
  34. tickets.sort(compare);
  35.  
  36. return tickets;
  37.  
  38. }
  39.  
  40. console.log(main(
  41. ['Philadelphia|94.20|available',
  42. 'New York City|95.99|available',
  43. 'New York City|95.99|sold',
  44. 'Boston|126.20|departed'],
  45. 'status'
  46. ));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement