Add comments to sorting.service

This commit is contained in:
Jakob Fahr 2020-07-12 22:25:33 +02:00
parent 0c1b112ea8
commit b3f4abdef6
No known key found for this signature in database
GPG Key ID: 8873416D8E4CEF6B

View File

@ -1,17 +1,38 @@
import {Priority} from './backend.service'; 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<T>(items: T[], key: (T) => number) { export function sortByNumberAscending<T>(items: T[], key: (T) => number) {
return items.sort((a, b) => key(a) - key(b)); 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<T>(items: T[], key: (T) => string) { export function sortByStringAscending<T>(items: T[], key: (T) => string) {
return items.sort((a, b) => key(a).localeCompare(key(b))); 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<T>(items: T[], key: (T) => Date) { export function sortByDateAscending<T>(items: T[], key: (T) => Date) {
return items.sort((a, b) => <any>key(b) - <any>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 { export function getNumberForPriority(priority: Priority): number {
switch (priority) { switch (priority) {
case Priority.High: case Priority.High:
@ -21,4 +42,4 @@ export function getNumberForPriority(priority: Priority): number {
case Priority.Low: case Priority.Low:
return 0; return 0;
} }
}; }