diff --git a/sort/sort.go b/sort/sort.go new file mode 100644 index 0000000..7abe526 --- /dev/null +++ b/sort/sort.go @@ -0,0 +1,18 @@ +package sort + +import "sort" + +// IntSlice attaches the methods of Interface to []T byte | int | int8 | int16 | int32 | int64 | uint | uintptr | uint16 | uint32 | uint64 | float32 | float64 +// , sorting in increasing order. +type NumberSlice[T byte | int | int8 | int16 | int32 | int64 | uint | uintptr | uint16 | uint32 | uint64 | float32 | float64] []T + +func (x NumberSlice[T]) Len() int { return len(x) } +func (x NumberSlice[T]) Less(i, j int) bool { return x[i] < x[j] } +func (x NumberSlice[T]) Swap(i, j int) { x[i], x[j] = x[j], x[i] } + +// Sort is a convenience method: x.Sort() calls Sort(x). +func (x NumberSlice[T]) Sort() { Sort(x) } + +func Sort[T byte | int | int8 | int16 | int32 | int64 | uint | uintptr | uint16 | uint32 | uint64 | float32 | float64](arr []T) { + sort.Sort(NumberSlice[T](arr)) +} diff --git a/sort/sort_test.go b/sort/sort_test.go new file mode 100644 index 0000000..dedd772 --- /dev/null +++ b/sort/sort_test.go @@ -0,0 +1,20 @@ +package sort + +import ( + "log" + "testing" +) + +func TestSort(t *testing.T) { + ints := []int{0, 1, 23, 4, 5, 6, 3} + Sort[int](ints) + log.Println(ints) + + uints := []uint{0, 1, 23, 4, 5, 6, 3} + Sort[uint](uints) + log.Println(uints) + + uint64s := []uint64{0, 1, 23, 4, 5, 6, 3} + Sort[uint64](uint64s) + log.Println(uint64s) +}