From 5e3e5a8351c0c533f7ec332c2a747f3e998b98f3 Mon Sep 17 00:00:00 2001 From: Amolith Date: Fri, 8 Aug 2025 15:08:40 -0600 Subject: [PATCH] =?UTF-8?q?perf:=20replace=20O(n=C2=B2)=20bubble=20sort=20?= =?UTF-8?q?with=20O(n=20log=20n)=20sort.Slice?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replace inefficient bubble sort implementation in formatTaskListWithFilter with Go's standard library sort.Slice for better performance and readability. - Improves time complexity from O(n²) to O(n log n) - Reduces code from 7 lines to 3 lines - Uses idiomatic Go standard library patterns - Maintains identical sorting behavior (oldest tasks first) - Passes all linting and static analysis checks --- internal/planning/manager.go | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/internal/planning/manager.go b/internal/planning/manager.go index 5ba14f8869d245a56125b7fcc6dc7df13623c704..3a9725395d5d50d3907dd166cb3fef7cc09e0406 100644 --- a/internal/planning/manager.go +++ b/internal/planning/manager.go @@ -7,6 +7,7 @@ package planning import ( "fmt" "log/slog" + "sort" "strings" "sync" "time" @@ -268,14 +269,10 @@ func (m *Manager) formatTaskListWithFilter(filterStatus *TaskStatus) string { } lines = append(lines, legend) - // Simple sort by creation time (newest first could be changed if needed) - for i := range len(taskList) { - for j := i + 1; j < len(taskList); j++ { - if taskList[i].CreatedAt.After(taskList[j].CreatedAt) { - taskList[i], taskList[j] = taskList[j], taskList[i] - } - } - } + // Sort tasks by creation time (oldest first for consistent ordering) + sort.Slice(taskList, func(i, j int) bool { + return taskList[i].CreatedAt.Before(taskList[j].CreatedAt) + }) for _, task := range taskList { line := fmt.Sprintf("%s %s [%s]", task.Status.String(), task.Title, task.ID)