Browse Source

ensure devices are returned in a consistent order

Mark deVilliers 2 years ago
parent
commit
5b412dc57f
1 changed files with 8 additions and 1 deletions
  1. 8 1
      node/api/devices.go

+ 8 - 1
node/api/devices.go

@@ -3,6 +3,7 @@ package api
3 3
 import (
4 4
 	"context"
5 5
 	"net/http"
6
+	"sort"
6 7
 
7 8
 	"gogs.dyne.org/DECODE/decode-prototype-da/node/sensors"
8 9
 	"gogs.dyne.org/DECODE/decode-prototype-da/utils"
@@ -144,7 +145,13 @@ func (e deviceResource) allDevices(request *restful.Request, response *restful.R
144 145
 	for _, v := range e.deviceStore {
145 146
 		resp = append(resp, v)
146 147
 	}
148
+	sort.Sort(byDeviceName(resp))
147 149
 
148 150
 	response.WriteEntity(resp)
149
-
150 151
 }
152
+
153
+type byDeviceName []DeviceResponse
154
+
155
+func (a byDeviceName) Len() int           { return len(a) }
156
+func (a byDeviceName) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
157
+func (a byDeviceName) Less(i, j int) bool { return a[i].Name < a[j].Name }