Commit 7ac2601c authored by imxyb's avatar imxyb

add uint32 slice sort

Signed-off-by: 's avatarimxyb <xyb4638@gmail.com>
parent afd79590
......@@ -29,6 +29,10 @@ func Int32(slice []int32) {
sort.Sort(Int32Slice(slice))
}
func Uint32(slice []uint32) {
sort.Sort(Uint32Slice(slice))
}
type Int64Slice []int64
func (p Int64Slice) Len() int {
......@@ -56,3 +60,17 @@ func (p Int32Slice) Less(i, j int) bool {
func (p Int32Slice) Swap(i, j int) {
p[i], p[j] = p[j], p[i]
}
type Uint32Slice []uint32
func (p Uint32Slice) Len() int {
return len(p)
}
func (p Uint32Slice) Less(i, j int) bool {
return p[i] < p[j]
}
func (p Uint32Slice) Swap(i, j int) {
p[i], p[j] = p[j], p[i]
}
......@@ -48,3 +48,15 @@ func TestSortInt64(t *testing.T) {
assert.Equal(t, int64(5), data[5])
assert.Equal(t, int64(9), data[6])
}
func TestSortUint32(t *testing.T) {
data := []uint32{3, 5, 1, 9, 0, 2, 2}
Uint32(data)
assert.Equal(t, uint32(0), data[0])
assert.Equal(t, uint32(1), data[1])
assert.Equal(t, uint32(2), data[2])
assert.Equal(t, uint32(2), data[3])
assert.Equal(t, uint32(3), data[4])
assert.Equal(t, uint32(5), data[5])
assert.Equal(t, uint32(9), data[6])
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment