Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { OlympiansFactory } from '../factories/olympians.factory.js';
- import { OlympicsCommittee } from '../providers/olympics-committee.js';
- export class ListOlympiansTask {
- #defaultKey = 'firstname';
- #defaultOrder = 'asc';
- #orderDirection = {
- asc: 1,
- desc: -1,
- };
- #factory = OlympiansFactory;
- #committee = OlympicsCommittee;
- run(key = this.#defaultKey, order = this.#defaultOrder) {
- if (this.#committee.olympians.length === 0) {
- return 'No olympians added!';
- }
- const final = [...this.#committee.olympians];
- if(this.#orderDirection[order] === 1) {
- final.sort((a, b) => a[key] > b[key] ? 1 : -1);
- }
- if(this.#orderDirection[order] === 1) {
- final.sort((a, b) => b[key] < a[key] ? 1 : -1);
- }
- let result = '';
- for(const el of final) {
- result += `${el.print()}\n`;
- }
- return `Sorted by [key: ${key}] in [order: ${order}]
- ${result}
- `;
- // this should return the sorted olympians info instead
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment