eternalmeg

03

Jun 21st, 2024
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. function solve(arr1, criteria) {
  2. let res = [];
  3.  
  4.  
  5. class Ticket {
  6. constructor(destination, price, status) {
  7. this.destination = destination;
  8. this.price = Number(price);
  9. this.status = status;
  10. }
  11. }
  12.  
  13. for (let i = 0; i < arr1.length; i++) {
  14. let ticketLine = arr1[i].split('|'); // Split each element in the array
  15. let [destination, price, status] = ticketLine; // Destructure the array elements
  16. let ticket = new Ticket(destination, price, status);
  17. res.push(ticket);
  18. }
  19.  
  20.  
  21. if (criteria === 'destination') {
  22. res.sort((a, b) => a.destination.localeCompare(b.destination));
  23. } else if (criteria === 'price') {
  24. res.sort((a, b) => a.price - b.price);
  25. } else if (criteria === 'status') {
  26. res.sort((a, b) => a.status.localeCompare(b.status));
  27. }
  28.  
  29. return res;
  30. }
  31.  
Advertisement
Add Comment
Please, Sign In to add comment