diff --git a/src/app/services/sorting.service.ts b/src/app/services/sorting.service.ts index 59b90e6..a3e471f 100644 --- a/src/app/services/sorting.service.ts +++ b/src/app/services/sorting.service.ts @@ -1,17 +1,38 @@ import {Priority} from './backend.service'; +/** + * Sort an array in place by a number associated with that item. + * @param items The items to sort. + * @param key Returns the number associated with an item. + */ export function sortByNumberAscending(items: T[], key: (T) => number) { return items.sort((a, b) => key(a) - key(b)); } +/** + * Sort an array in place by a string associated with that item. + * Strings will be sorted in alphabetic order. + * @param items The items to sort. + * @param key Returns the string associated with an item. + */ export function sortByStringAscending(items: T[], key: (T) => string) { return items.sort((a, b) => key(a).localeCompare(key(b))); } +/** + * Sort an array in place by a date associated with that item. + * Dates will be sorted from old to new. + * @param items The items to sort. + * @param key Returns the date associated with an item. + */ export function sortByDateAscending(items: T[], key: (T) => Date) { - return items.sort((a, b) => key(b) - key(a)); + return items.sort((a, b) => (key(b) as any) - (key(a) as any)); } +/** + * Converts a priority enum member to a number. + * @returns high -> 2, medium -> 1, low -> 0 + */ export function getNumberForPriority(priority: Priority): number { switch (priority) { case Priority.High: @@ -21,4 +42,4 @@ export function getNumberForPriority(priority: Priority): number { case Priority.Low: return 0; } -}; +}