srumboard_frontend/src/app/services/sorting.service.ts
2020-07-12 22:25:33 +02:00

46 lines
1.3 KiB
TypeScript

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) {
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) {
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) {
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:
return 2;
case Priority.Medium:
return 1;
case Priority.Low:
return 0;
}
}