-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
name
committed
Oct 24, 2022
1 parent
4991341
commit fd509a9
Showing
2 changed files
with
38 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |