From 3e4f7fb2f472ada4a2cb4e1322b06113a30809dc Mon Sep 17 00:00:00 2001 From: konrad Date: Wed, 19 Dec 2018 19:14:48 +0000 Subject: [PATCH] Fix sorting of tasks in lists (#37) --- pkg/models/list_tasks.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/pkg/models/list_tasks.go b/pkg/models/list_tasks.go index 2d5f356549..f58f339bdd 100644 --- a/pkg/models/list_tasks.go +++ b/pkg/models/list_tasks.go @@ -18,6 +18,7 @@ package models import ( "code.vikunja.io/web" + "sort" ) // ListTask represents an task in a todolist @@ -64,7 +65,7 @@ func GetTasksByListID(listID int64) (tasks []*ListTask, err error) { } // make a map so we can put in subtasks more easily - taskMap := make(map[int64]*ListTask) + taskMap := make(map[int64]*ListTask, len(tasks)) // Get all users and put them into the array var userIDs []int64 @@ -114,6 +115,13 @@ func GetTasksByListID(listID int64) (tasks []*ListTask, err error) { tasks = append(tasks, t) } + // Sort the output. In Go, contents on a map are put on that map in no particular order. + // Because of this, tasks are not sorted anymore in the output, this leads to confiusion. + // To avoid all this, we need to sort the slice afterwards + sort.Slice(tasks, func(i, j int) bool { + return tasks[i].ID < tasks[j].ID + }) + return }