You can specify an alias of type [] uint64, add standard "template" sorting methods to implement sort.interface ( Len , Swap and Less - https://golang.org/pkg/sort/#Interface ); either create an instance of a new type, or create an existing slice [] of uint64 into a new type, as was done in the following example (also https://play.golang.org/p/BbB3L9TmBI ):
package main import ( "fmt" "sort" ) type uint64arr []uint64 func (a uint64arr) Len() int { return len(a) } func (a uint64arr) Swap(i, j int) { a[i], a[j] = a[j], a[i] } func (a uint64arr) Less(i, j int) bool { return a[i] < a[j] } func (a uint64arr) String() (s string) { sep := "" // for printing separating commas for _, el := range a { s += sep sep = ", " s += fmt.Sprintf("%d", el) } return } func main() { dirRange := []uint64{3, 2, 400000} arr := uint64arr(dirRange) sort.Sort(arr) fmt.Printf("%s\n", arr) fmt.Printf("%#v\n", dirRange) }
Output:
2, 3, 400000 []uint64{0x2, 0x3, 0x61a80}
indicating that both arrays are sorted, since the second is an alias with a drive for the original.
dmitris
source share