@@ -17,9 +17,11 @@ package board
1717
1818import (
1919 "context"
20+ "sort"
2021 "strings"
2122
2223 "github.com/arduino/arduino-cli/arduino"
24+ "github.com/arduino/arduino-cli/arduino/cores"
2325 "github.com/arduino/arduino-cli/arduino/utils"
2426 "github.com/arduino/arduino-cli/commands"
2527 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
3638 searchArgs := strings .Join (req .GetSearchArgs (), " " )
3739
3840 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 ) {
4143 installedPlatformRelease := pme .GetInstalledPlatformRelease (platform )
4244 // We only want to list boards for installed platforms
4345 if installedPlatformRelease == nil {
@@ -93,3 +95,37 @@ func ListAll(ctx context.Context, req *rpc.BoardListAllRequest) (*rpc.BoardListA
9395
9496 return list , nil
9597}
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