Skip to content

Commit a20757f

Browse files
authored
Make sorting more readable (#1128)
1 parent b5c4fc3 commit a20757f

File tree

2 files changed

+63
-27
lines changed

2 files changed

+63
-27
lines changed

clusters/data_node_type.go

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package clusters
22

33
import (
44
"context"
5-
"sort"
65
"strings"
76

87
"github.com/databrickslabs/terraform-provider-databricks/common"
@@ -32,33 +31,21 @@ type NodeTypeList struct {
3231

3332
// Sort NodeTypes within this struct
3433
func (l *NodeTypeList) Sort() {
35-
sort.Slice(l.NodeTypes, func(i, j int) bool {
36-
if l.NodeTypes[i].IsDeprecated != l.NodeTypes[j].IsDeprecated {
37-
return !l.NodeTypes[i].IsDeprecated
34+
sortByChain(l.NodeTypes, func(i int) sortCmp {
35+
var localDisks, localDiskSizeGB int32
36+
if l.NodeTypes[i].NodeInstanceType != nil {
37+
localDisks = l.NodeTypes[i].NodeInstanceType.LocalDisks
38+
localDiskSizeGB = l.NodeTypes[i].NodeInstanceType.LocalDiskSizeGB
39+
}
40+
return sortChain{
41+
boolAsc(l.NodeTypes[i].IsDeprecated),
42+
intAsc(localDisks),
43+
intAsc(localDiskSizeGB),
44+
intAsc(l.NodeTypes[i].MemoryMB),
45+
intAsc(l.NodeTypes[i].NumCores),
46+
intAsc(l.NodeTypes[i].NumGPUs),
47+
strAsc(l.NodeTypes[i].InstanceTypeID),
3848
}
39-
if l.NodeTypes[i].NodeInstanceType != nil &&
40-
l.NodeTypes[j].NodeInstanceType != nil {
41-
if l.NodeTypes[i].NodeInstanceType.LocalDisks !=
42-
l.NodeTypes[j].NodeInstanceType.LocalDisks {
43-
return l.NodeTypes[i].NodeInstanceType.LocalDisks <
44-
l.NodeTypes[j].NodeInstanceType.LocalDisks
45-
}
46-
if l.NodeTypes[i].NodeInstanceType.LocalDiskSizeGB !=
47-
l.NodeTypes[j].NodeInstanceType.LocalDiskSizeGB {
48-
return l.NodeTypes[i].NodeInstanceType.LocalDiskSizeGB <
49-
l.NodeTypes[j].NodeInstanceType.LocalDiskSizeGB
50-
}
51-
}
52-
if l.NodeTypes[i].MemoryMB != l.NodeTypes[j].MemoryMB {
53-
return l.NodeTypes[i].MemoryMB < l.NodeTypes[j].MemoryMB
54-
}
55-
if l.NodeTypes[i].NumCores != l.NodeTypes[j].NumCores {
56-
return l.NodeTypes[i].NumCores < l.NodeTypes[j].NumCores
57-
}
58-
if l.NodeTypes[i].NumGPUs != l.NodeTypes[j].NumGPUs {
59-
return l.NodeTypes[i].NumGPUs < l.NodeTypes[j].NumGPUs
60-
}
61-
return l.NodeTypes[i].InstanceTypeID < l.NodeTypes[j].InstanceTypeID
6249
})
6350
}
6451

clusters/sort.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package clusters
2+
3+
import (
4+
"sort"
5+
)
6+
7+
// readable chained sorting helper
8+
func sortByChain(s interface{}, fn func(int) sortCmp) {
9+
sort.Slice(s, func(i, j int) bool {
10+
return fn(i).Less(fn(j))
11+
})
12+
}
13+
14+
type sortCmp interface {
15+
Less(o sortCmp) bool
16+
}
17+
18+
type boolAsc bool
19+
20+
func (b boolAsc) Less(o sortCmp) bool {
21+
return bool(b) != bool(o.(boolAsc)) && !bool(b)
22+
}
23+
24+
type intAsc int
25+
26+
func (ia intAsc) Less(o sortCmp) bool {
27+
return int(ia) < int(o.(intAsc))
28+
}
29+
30+
type strAsc string
31+
32+
func (s strAsc) Less(o sortCmp) bool {
33+
return string(s) < string(o.(strAsc))
34+
}
35+
36+
type sortChain []sortCmp
37+
38+
func (c sortChain) Less(other sortCmp) bool {
39+
o := other.(sortChain)
40+
for i := range c {
41+
if c[i].Less(o[i]) {
42+
return true
43+
}
44+
if o[i].Less(c[i]) {
45+
break
46+
}
47+
}
48+
return false
49+
}

0 commit comments

Comments
 (0)