8
8
"errors"
9
9
"fmt"
10
10
"net/http"
11
+ "sort"
11
12
"time"
12
13
13
14
"github.com/go-chi/chi/v5"
@@ -673,6 +674,7 @@ func getCreatedByNamesByTemplateIDs(ctx context.Context, db database.Store, temp
673
674
674
675
func convertTemplates (templates []database.Template ,workspaceCounts []database.GetWorkspaceOwnerCountsByTemplateIDsRow ,createdByNameMap map [string ]string ) []codersdk.Template {
675
676
apiTemplates := make ([]codersdk.Template ,0 ,len (templates ))
677
+
676
678
for _ ,template := range templates {
677
679
found := false
678
680
for _ ,workspaceCount := range workspaceCounts {
@@ -687,6 +689,12 @@ func convertTemplates(templates []database.Template, workspaceCounts []database.
687
689
apiTemplates = append (apiTemplates ,convertTemplate (template ,uint32 (0 ),createdByNameMap [template .ID .String ()]))
688
690
}
689
691
}
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
+
690
698
return apiTemplates
691
699
}
692
700