@@ -2,6 +2,7 @@ package environments
22
33import (
44 "net/http"
5+ "sort"
56
67 "github.com/gin-gonic/gin"
78
@@ -60,6 +61,26 @@ func (s *Environments) ListEnvironments(c *gin.Context, workspaceId string, para
6061 environmentsList = append (environmentsList , environment )
6162 }
6263
64+ sort .Slice (environmentsList , func (i , j int ) bool {
65+ if environmentsList [i ] == nil && environmentsList [j ] == nil {
66+ return false
67+ }
68+ if environmentsList [i ] == nil {
69+ return false
70+ }
71+ if environmentsList [j ] == nil {
72+ return true
73+ }
74+ if environmentsList [i ].Name < environmentsList [j ].Name {
75+ return true
76+ }
77+ if environmentsList [i ].Name > environmentsList [j ].Name {
78+ return false
79+ }
80+ // Names are equal; compare Id
81+ return environmentsList [i ].Id < environmentsList [j ].Id
82+ })
83+
6384 c .JSON (http .StatusOK , gin.H {
6485 "total" : total ,
6586 "offset" : offset ,
@@ -90,6 +111,26 @@ func (s *Environments) GetEnvironmentResources(c *gin.Context, workspaceId strin
90111 resourceList = append (resourceList , resource )
91112 }
92113
114+ sort .Slice (resourceList , func (i , j int ) bool {
115+ if resourceList [i ] == nil && resourceList [j ] == nil {
116+ return false
117+ }
118+ if resourceList [i ] == nil {
119+ return false
120+ }
121+ if resourceList [j ] == nil {
122+ return true
123+ }
124+ if resourceList [i ].Name < resourceList [j ].Name {
125+ return true
126+ }
127+ if resourceList [i ].Name > resourceList [j ].Name {
128+ return false
129+ }
130+ // Names are equal; compare Id
131+ return resourceList [i ].Id < resourceList [j ].Id
132+ })
133+
93134 // Get pagination parameters with defaults
94135 limit := 50
95136 if params .Limit != nil {
@@ -103,14 +144,8 @@ func (s *Environments) GetEnvironmentResources(c *gin.Context, workspaceId strin
103144 total := len (resourceList )
104145
105146 // Apply pagination
106- start := offset
107- if start > total {
108- start = total
109- }
110- end := start + limit
111- if end > total {
112- end = total
113- }
147+ start := min (offset , total )
148+ end := min (start + limit , total )
114149 paginatedResources := resourceList [start :end ]
115150
116151 c .JSON (http .StatusOK , gin.H {
0 commit comments