From c8d9c44abad44f178184e7d4e37092e944417415 Mon Sep 17 00:00:00 2001 From: Kyle Carberry Date: Wed, 7 Sep 2022 16:16:53 -0500 Subject: [PATCH] fix: Sort workspaces by last used then name (#3943) --- coderd/workspaces.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/coderd/workspaces.go b/coderd/workspaces.go index fa9e0784a2..2c787407e6 100644 --- a/coderd/workspaces.go +++ b/coderd/workspaces.go @@ -8,6 +8,7 @@ import ( "fmt" "net/http" "net/url" + "sort" "strconv" "strings" "time" @@ -913,6 +914,15 @@ func convertWorkspaces(ctx context.Context, db database.Store, workspaces []data } apiWorkspaces = append(apiWorkspaces, convertWorkspace(workspace, build, job, template, &owner, &initiator)) } + sort.Slice(apiWorkspaces, func(i, j int) bool { + iw := apiWorkspaces[i] + jw := apiWorkspaces[j] + if jw.LastUsedAt.IsZero() && iw.LastUsedAt.IsZero() { + return iw.Name < jw.Name + } + return iw.LastUsedAt.After(jw.LastUsedAt) + }) + return apiWorkspaces, nil }