Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- tickets = (data = [], criteria = '') => {
- class Ticket {
- constructor(destination, price, status) {
- this.destination = destination;
- this.price = Number(price);
- this.status = status;
- }
- }
- const statuses = {
- 'available': 1,
- 'departed': 2,
- 'sold': 3
- };
- const sortAlpabetically = (str1, str2) => {
- return str1.localeCompare(str2);
- };
- const sortTickets = (a, b) => {
- if(criteria === 'price') {
- return a.price - b.price;
- } else if(criteria === 'status') {
- return statuses[a.status] - statuses[b.status];
- }
- return sortAlpabetically(a.destination, b.destination);
- };
- return data
- .map(str => new Ticket(...str.split('|')))
- .sort(sortTickets || sortAlpabetically(a.destination, b.destination));
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement