Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solve(tickets,criteria){
- // const comparator = {
- // destination: (a,b) => (a.destination.localeCompare(b.destination)),
- // price: (a,b) => (a - b),
- // status: (a,b) => (a.status.localeCompare(b.status))
- // }
- class Ticket {
- constructor(descriptor){
- const [destination,price,status] = descriptor.split("|");
- this.destination = destination,
- this.price = +price,
- this.status = status
- }
- }
- return tickets.map(t=> new Ticket(t)).sort(comparator[criteria]);
- function comparator(a,b){
- try{
- return a[criteria].localeCompare(b[criteria])
- }catch{
- return a[criteria] - b[criteria];
- }
- }
- }
- console.log(solve(
- // ['Philadelphia|94.20|available',
- // 'New York City|95.99|available',
- // 'New York City|95.99|sold',
- // 'Boston|126.20|departed'],
- // 'destination'
- ['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