From 7549d4e1e3752ea8b0aa725b0f59942fcd332091 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Wed, 2 Aug 2023 13:23:44 +0200 Subject: [PATCH] Create `groupAndSortTasks` function --- .../app/board/[board]/groupAndSortTasks.ts | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 todoblue/src/app/board/[board]/groupAndSortTasks.ts diff --git a/todoblue/src/app/board/[board]/groupAndSortTasks.ts b/todoblue/src/app/board/[board]/groupAndSortTasks.ts new file mode 100644 index 0000000..e3d56ef --- /dev/null +++ b/todoblue/src/app/board/[board]/groupAndSortTasks.ts @@ -0,0 +1,20 @@ +import {Task} from "@/app/board/[board]/types" + + +export function groupAndSortTasks(tasks: Task[], grouping: (a: Task) => string, sorting: (a: Task, b: Task) => number) { + const groups: {[group: string]: Task[]} = {} + + for(const task of tasks) { + const group = grouping(task); + if(!groups[group]) { + groups[group] = []; + } + groups[group].push(task); + } + + for(const group of Object.keys(groups)) { + groups[group].sort(sorting); + } + + return groups; +}