Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function main(arr, dest) {
- class Ticket {
- constructor(destination,price,status) {
- this.destination=destination;
- this.price=Number(price);
- this.status=status;
- }
- }
- let tickets=[];
- for (let obj of arr) {
- let [destination,price,status]=obj.split('|');
- let ticket= new Ticket(destination,price,status);
- tickets.push(ticket);
- }
- //Sorting
- function compare(a, b) {
- const ticketA = a[dest];
- const ticketB = b[dest];
- let comparison = 0;
- if (ticketA > ticketB) {
- comparison = 1;
- } else if (ticketA < ticketB) {
- comparison = -1;
- }
- return comparison;
- }
- tickets.sort(compare);
- return tickets;
- }
- console.log(main(
- ['Philadelphia|94.20|available',
- 'New York City|95.99|available',
- 'New York City|95.99|sold',
- 'Boston|126.20|departed'],
- 'status'
- ));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement