Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(arr1, criteria) {
- let res = [];
- class Ticket {
- constructor(destination, price, status) {
- this.destination = destination;
- this.price = Number(price);
- this.status = status;
- }
- }
- for (let i = 0; i < arr1.length; i++) {
- let ticketLine = arr1[i].split('|'); // Split each element in the array
- let [destination, price, status] = ticketLine; // Destructure the array elements
- let ticket = new Ticket(destination, price, status);
- res.push(ticket);
- }
- if (criteria === 'destination') {
- res.sort((a, b) => a.destination.localeCompare(b.destination));
- } else if (criteria === 'price') {
- res.sort((a, b) => a.price - b.price);
- } else if (criteria === 'status') {
- res.sort((a, b) => a.status.localeCompare(b.status));
- }
- return res;
- }
Advertisement
Add Comment
Please, Sign In to add comment