cluster: sort regions, for `jpictl dump` sake #37
Merged
amery
merged 2 commits from pr-amery-region-sort
into main
1 year ago
3 changed files with 46 additions and 27 deletions
@ -0,0 +1,41 @@
|
||||
package cluster |
||||
|
||||
import "sort" |
||||
|
||||
// SortRegions sorts regions. first by length those 3-character
|
||||
// or shorter, and then by length. It's mostly aimed at
|
||||
// supporting ISO-3166 order
|
||||
func SortRegions(regions []string) []string { |
||||
sort.Slice(regions, func(i, j int) bool { |
||||
r1, r2 := regions[i], regions[j] |
||||
return regionLess(r1, r2) |
||||
}) |
||||
|
||||
return regions |
||||
} |
||||
|
||||
func regionLess(r1, r2 string) bool { |
||||
switch { |
||||
case len(r1) < 4: |
||||
switch { |
||||
case len(r1) < len(r2): |
||||
return true |
||||
case len(r1) > len(r2): |
||||
return false |
||||
default: |
||||
return r1 < r2 |
||||
} |
||||
case len(r2) < 4: |
||||
return false |
||||
default: |
||||
return r1 < r2 |
||||
} |
||||
} |
||||
|
||||
func (m *Cluster) sortRegions() { |
||||
sort.Slice(m.Regions, func(i, j int) bool { |
||||
r1 := m.Regions[i].Name |
||||
r2 := m.Regions[j].Name |
||||
return regionLess(r1, r2) |
||||
}) |
||||
} |
Loading…
Reference in new issue