Skip to content

Commit

Permalink
feat: refactor sort filters, aggregate default sort by start date
Browse files Browse the repository at this point in the history
  • Loading branch information
KevinWu098 committed Apr 25, 2024
1 parent feda4bc commit b2f9444
Showing 1 changed file with 39 additions and 22 deletions.
61 changes: 39 additions & 22 deletions lib/utils/filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,26 +54,43 @@ export function filterData(
);
});

const sortedResults =
filterValues.sort == "Alphabetical"
? filteredResults.sort((courseA, courseB) => {
const nameA = courseA.courseCode + courseA.courseName;
const nameB = courseB.courseCode + courseB.courseName;

return nameA.localeCompare(nameB);
})
: filterValues.sort == "Tuition"
? filteredResults.sort((courseA, courseB) => {
return courseA.tuition - courseB.tuition;
})
: filterValues.sort == "Shortest Term"
? filteredResults.sort((courseA, courseB) => {
const termLengthA = courseA.endDate - courseA.startDate;
const termLengthB = courseB.endDate - courseB.startDate;

return termLengthA - termLengthB;
})
: filteredResults;

return sortedResults;
const sort = filterValues.sort;

if (sort === "Alphabetical") {
filteredResults.sort((courseA, courseB) => {
const nameA = courseA.courseCode + courseA.courseName;
const nameB = courseB.courseCode + courseB.courseName;

return nameA.localeCompare(nameB);
});
} else if (sort === "Tuition") {
filteredResults.sort((courseA, courseB) => {
return courseA.tuition - courseB.tuition;
});
} else if (sort === "Shortest Term") {
filteredResults.sort((courseA, courseB) => {
const termLengthA = courseA.endDate - courseA.startDate;
const termLengthB = courseB.endDate - courseB.startDate;

return termLengthA - termLengthB;
});
} else {
// Default Sort pushes past courses to the bottom of the list

const pastCourses = filteredResults.filter(
(course) => new Date(course.startDate) <= new Date(),
);

const futureCourses = filteredResults.filter(
(course) => new Date(course.startDate) > new Date(),
);

const dateAggregatedCourses = [];
dateAggregatedCourses.push(...futureCourses);
dateAggregatedCourses.push(...pastCourses);

return dateAggregatedCourses;
}

return filteredResults;
}

0 comments on commit b2f9444

Please sign in to comment.