Skip to content

Commit

Permalink
task solution
Browse files Browse the repository at this point in the history
  • Loading branch information
W3zzie committed Nov 13, 2024
1 parent ad2a516 commit b4234e6
Showing 1 changed file with 52 additions and 4 deletions.
56 changes: 52 additions & 4 deletions src/sortStudents.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,64 @@

export interface Student {
// describe Student interface
name: string;
surname: string;
age: number;
married: boolean;
grades: number[];
}

export enum SortType {
// describe SortType enum
Name = 'name',
Surname = 'surname',
Age = 'age',
Married = 'married',
AverageGrade = 'grades',
}

// create SortOrder type
export type SortOrder;
export type SortOrder = 'asc' | 'desc';

export function sortStudents(
students: Student[],
sortBy: SortType,
order: SortOrder,
): Student[] {
const sortedStudents = [...students];

function getAverageGrades(student: Student): number {
return (
student.grades.reduce((sum, grade) => sum + grade, 0)
/ student.grades.length
);
}

switch (sortBy) {
case SortType.Surname:
case SortType.Name:
sortedStudents.sort((a, b) => {
return order === 'asc'
? a[sortBy].localeCompare(b[sortBy])
: b[sortBy].localeCompare(a[sortBy]);
});
break;

case SortType.AverageGrade:
sortedStudents.sort((a, b) => {
return order === 'asc'
? getAverageGrades(a) - getAverageGrades(b)
: getAverageGrades(b) - getAverageGrades(a);
});
break;

default:
sortedStudents.sort((a, b) => {
return order === 'asc'
? +a[sortBy] - +b[sortBy]
: +b[sortBy] - +a[sortBy];
});
break;
}

export function sortStudents(students, sortBy, order) {
// write your function
return sortedStudents;
}

0 comments on commit b4234e6

Please sign in to comment.