Skip to content

Instantly share code, notes, and snippets.

@lzap
Created July 26, 2023 14:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lzap/430887e057e4b188f154d24f1f2a9626 to your computer and use it in GitHub Desktop.
Save lzap/430887e057e4b188f154d24f1f2a9626 to your computer and use it in GitHub Desktop.
Sortable HardwareAddr Golang
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