From 66e614304abec6132c88c3fe84f99dd9f960c90f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20B=C3=BCtow?= <1224732+snonux@users.noreply.github.com> Date: Fri, 20 Jun 2025 21:35:01 +0300 Subject: sort: prioritize started tasks --- internal/task/sort_test.go | 20 ++++++++++++++++++++ internal/task/task.go | 11 +++++++++-- 2 files changed, 29 insertions(+), 2 deletions(-) (limited to 'internal/task') diff --git a/internal/task/sort_test.go b/internal/task/sort_test.go index 10c1a12..056a389 100644 --- a/internal/task/sort_test.go +++ b/internal/task/sort_test.go @@ -26,3 +26,23 @@ func TestSortTasks(t *testing.T) { t.Fatalf("unexpected order: %v", ids) } } + +func TestSortTasksStartedFirst(t *testing.T) { + tasks := []Task{ + {ID: 1, Priority: "M", Start: "20240101T000000Z"}, + {ID: 2, Priority: "H"}, + {ID: 3, Priority: "H", Start: "20240102T000000Z"}, + {ID: 4, Priority: "L"}, + } + + SortTasks(tasks) + + var ids []int + for _, tsk := range tasks { + ids = append(ids, tsk.ID) + } + want := []int{3, 1, 2, 4} + if !reflect.DeepEqual(ids, want) { + t.Fatalf("unexpected order: %v", ids) + } +} diff --git a/internal/task/task.go b/internal/task/task.go index 4bd72c1..9c715c8 100644 --- a/internal/task/task.go +++ b/internal/task/task.go @@ -240,8 +240,9 @@ func Edit(id int) error { return EditCmd(id).Run() } -// SortTasks orders tasks by priority, due date, tag names and id. -// Tasks without a due date are placed after tasks with a due date. +// SortTasks orders tasks by start status, priority, due date, tag names and id. +// Started tasks are always placed before non-started ones. Tasks without a due +// date are placed after tasks with a due date. func SortTasks(tasks []Task) { joinTags := func(tags []string) string { if len(tags) == 0 { @@ -279,6 +280,12 @@ func SortTasks(tasks []Task) { sort.Slice(tasks, func(i, j int) bool { ti, tj := tasks[i], tasks[j] + startedI := ti.Start != "" && ti.Status != "completed" + startedJ := tj.Start != "" && tj.Status != "completed" + if startedI != startedJ { + return startedI + } + pi, pj := priVal(ti.Priority), priVal(tj.Priority) if pi != pj { return pi > pj -- cgit v1.2.3