From 196c7b7873c19e5f9907458772d34009ff00cd27 Mon Sep 17 00:00:00 2001 From: Maciej Ziarkowski Date: Mon, 27 Jan 2020 01:01:18 +0000 Subject: [PATCH] Add sorting helpers --- app/src/helpers.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/app/src/helpers.ts b/app/src/helpers.ts index 372001e1..6f156561 100644 --- a/app/src/helpers.ts +++ b/app/src/helpers.ts @@ -34,3 +34,15 @@ export function isNullishOrEmpty(obj: any) { export function isEmptyArray(obj: any) { return Array.isArray(obj) && obj.length === 0; } + +type AccessorFunction = (obj: T) => V; + +type CompareFunction = (a: T, b: T) => number; + +export function numAsc(accessor: AccessorFunction): CompareFunction{ + return (a: T, b: T) => Number(accessor(a) - accessor(b)); +} + +export function numDesc(accessor: AccessorFunction): CompareFunction { + return (a: T, b: T) => Number(accessor(b) - accessor(a)); +}