Skip to content

Commit 0708e37

Browse files
feat: Sort templates by workspaces count (#3734)
1 parent 1903104 commit 0708e37

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

coderd/templates.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"errors"
99
"fmt"
1010
"net/http"
11+
"sort"
1112
"time"
1213

1314
"github.com/go-chi/chi/v5"
@@ -673,6 +674,7 @@ func getCreatedByNamesByTemplateIDs(ctx context.Context, db database.Store, temp
673674

674675
func convertTemplates(templates []database.Template, workspaceCounts []database.GetWorkspaceOwnerCountsByTemplateIDsRow, createdByNameMap map[string]string) []codersdk.Template {
675676
apiTemplates := make([]codersdk.Template, 0, len(templates))
677+
676678
for _, template := range templates {
677679
found := false
678680
for _, workspaceCount := range workspaceCounts {
@@ -687,6 +689,12 @@ func convertTemplates(templates []database.Template, workspaceCounts []database.
687689
apiTemplates = append(apiTemplates, convertTemplate(template, uint32(0), createdByNameMap[template.ID.String()]))
688690
}
689691
}
692+
693+
// Sort templates by WorkspaceOwnerCount DESC
694+
sort.SliceStable(apiTemplates, func(i, j int) bool {
695+
return apiTemplates[i].WorkspaceOwnerCount > apiTemplates[j].WorkspaceOwnerCount
696+
})
697+
690698
return apiTemplates
691699
}
692700

0 commit comments

Comments
 (0)