From 0708e37a38ce1fbc74803d3ba44354149db294ad Mon Sep 17 00:00:00 2001 From: Bruno Quaresma Date: Tue, 30 Aug 2022 14:27:33 -0300 Subject: [PATCH] feat: Sort templates by workspaces count (#3734) --- coderd/templates.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/coderd/templates.go b/coderd/templates.go index eee18e0be1..e43fdb61e0 100644 --- a/coderd/templates.go +++ b/coderd/templates.go @@ -8,6 +8,7 @@ import ( "errors" "fmt" "net/http" + "sort" "time" "github.com/go-chi/chi/v5" @@ -673,6 +674,7 @@ func getCreatedByNamesByTemplateIDs(ctx context.Context, db database.Store, temp func convertTemplates(templates []database.Template, workspaceCounts []database.GetWorkspaceOwnerCountsByTemplateIDsRow, createdByNameMap map[string]string) []codersdk.Template { apiTemplates := make([]codersdk.Template, 0, len(templates)) + for _, template := range templates { found := false for _, workspaceCount := range workspaceCounts { @@ -687,6 +689,12 @@ func convertTemplates(templates []database.Template, workspaceCounts []database. apiTemplates = append(apiTemplates, convertTemplate(template, uint32(0), createdByNameMap[template.ID.String()])) } } + + // Sort templates by WorkspaceOwnerCount DESC + sort.SliceStable(apiTemplates, func(i, j int) bool { + return apiTemplates[i].WorkspaceOwnerCount > apiTemplates[j].WorkspaceOwnerCount + }) + return apiTemplates }