Created
July 26, 2023 14:56
-
-
Save lzap/430887e057e4b188f154d24f1f2a9626 to your computer and use it in GitHub Desktop.
Sortable HardwareAddr Golang
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type HwAddrSlice []net.HardwareAddr | |
func (s HwAddrSlice) Len() int { | |
return len(s) | |
} | |
func (s HwAddrSlice) Swap(i, j int) { | |
s[i], s[j] = s[j], s[i] | |
} | |
// Less sorts hardware addresses in a way that longer (EUI-48/64 or Infiniband) comes first and | |
// addresses of the same length are sorted by alphabetical (hex) order. | |
func (s HwAddrSlice) Less(i, j int) bool { | |
if len(s[i]) > len(s[j]) { | |
return true | |
} | |
for k := len(s[i]) - 1; k >= 0; k-- { | |
if s[i][k] > s[j][k] { | |
return false | |
} | |
} | |
return true | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment