@@ -17,9 +17,11 @@ package board
17
17
18
18
import (
19
19
"context"
20
+ "sort"
20
21
"strings"
21
22
22
23
"github.com/arduino/arduino-cli/arduino"
24
+ "github.com/arduino/arduino-cli/arduino/cores"
23
25
"github.com/arduino/arduino-cli/arduino/utils"
24
26
"github.com/arduino/arduino-cli/commands"
25
27
rpc "github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1"
@@ -36,8 +38,8 @@ func ListAll(ctx context.Context, req *rpc.BoardListAllRequest) (*rpc.BoardListA
36
38
searchArgs := strings .Join (req .GetSearchArgs (), " " )
37
39
38
40
list := & rpc.BoardListAllResponse {Boards : []* rpc.BoardListItem {}}
39
- for _ , targetPackage := range pme .GetPackages () {
40
- for _ , platform := range targetPackage .Platforms {
41
+ for _ , targetPackage := range toSortedPackageArray ( pme .GetPackages () ) {
42
+ for _ , platform := range toSortedPlatformArray ( targetPackage .Platforms ) {
41
43
installedPlatformRelease := pme .GetInstalledPlatformRelease (platform )
42
44
// We only want to list boards for installed platforms
43
45
if installedPlatformRelease == nil {
@@ -93,3 +95,37 @@ func ListAll(ctx context.Context, req *rpc.BoardListAllRequest) (*rpc.BoardListA
93
95
94
96
return list , nil
95
97
}
98
+
99
+ // TODO use a generic function instead of the two below once go >1.18 is adopted.
100
+ // Without generics we either have to create multiple functions for different map types
101
+ // or resort to type assertions on the caller side
102
+
103
+ // toSortedPackageArray takes a packages map and returns its values as array
104
+ // ordered by the map keys alphabetically
105
+ func toSortedPackageArray (sourceMap cores.Packages ) []* cores.Package {
106
+ keys := []string {}
107
+ for key := range sourceMap {
108
+ keys = append (keys , key )
109
+ }
110
+ sort .Strings (keys )
111
+ sortedValues := make ([]* cores.Package , len (keys ))
112
+ for i , key := range keys {
113
+ sortedValues [i ] = sourceMap [key ]
114
+ }
115
+ return sortedValues
116
+ }
117
+
118
+ // toSortedPlatformArray takes a packages map and returns its values as array
119
+ // ordered by the map keys alphabetically
120
+ func toSortedPlatformArray (sourceMap map [string ]* cores.Platform ) []* cores.Platform {
121
+ keys := []string {}
122
+ for key := range sourceMap {
123
+ keys = append (keys , key )
124
+ }
125
+ sort .Strings (keys )
126
+ sortedValues := make ([]* cores.Platform , len (keys ))
127
+ for i , key := range keys {
128
+ sortedValues [i ] = sourceMap [key ]
129
+ }
130
+ return sortedValues
131
+ }
0 commit comments