github.com/go-board/x-go@v0.1.2-0.20220610024734-db1323f6cb15/xsort/sort.go (about) 1 package xsort 2 3 import ( 4 "sort" 5 6 "github.com/go-board/x-go/types" 7 ) 8 9 // isNaN64 is a copy of math.IsNaN to avoid a dependency on the math package. 10 func isNaN64(f float64) bool { return f != f } 11 12 // isNaN32 is a copy of math.IsNaN to avoid a dependency on the math package. 13 func isNaN32(f float32) bool { return f != f } 14 15 type ByteSlice types.ByteSlice 16 17 func (p ByteSlice) Len() int { return len(p) } 18 func (p ByteSlice) Less(i, j int) bool { return p[i] < p[j] } 19 func (p ByteSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] } 20 func (p ByteSlice) Sort() { sort.Sort(p) } 21 22 type UintSlice types.UintSlice 23 24 func (p UintSlice) Len() int { return len(p) } 25 func (p UintSlice) Less(i, j int) bool { return p[i] < p[j] } 26 func (p UintSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] } 27 func (p UintSlice) Sort() { sort.Sort(p) } 28 29 type Uint64Slice types.Uint64Slice 30 31 func (p Uint64Slice) Len() int { return len(p) } 32 func (p Uint64Slice) Less(i, j int) bool { return p[i] < p[j] } 33 func (p Uint64Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] } 34 func (p Uint64Slice) Sort() { sort.Sort(p) } 35 36 type Uint32Slice types.Uint32Slice 37 38 func (p Uint32Slice) Len() int { return len(p) } 39 func (p Uint32Slice) Less(i, j int) bool { return p[i] < p[j] } 40 func (p Uint32Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] } 41 func (p Uint32Slice) Sort() { sort.Sort(p) } 42 43 type Uint16Slice types.Uint16Slice 44 45 func (p Uint16Slice) Len() int { return len(p) } 46 func (p Uint16Slice) Less(i, j int) bool { return p[i] < p[j] } 47 func (p Uint16Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] } 48 func (p Uint16Slice) Sort() { sort.Sort(p) } 49 50 type Uint8Slice types.Uint8Slice 51 52 func (p Uint8Slice) Len() int { return len(p) } 53 func (p Uint8Slice) Less(i, j int) bool { return p[i] < p[j] } 54 func (p Uint8Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] } 55 func (p Uint8Slice) Sort() { sort.Sort(p) } 56 57 type IntSlice = sort.IntSlice 58 59 type Int64Slice types.Int64Slice 60 61 func (p Int64Slice) Len() int { return len(p) } 62 func (p Int64Slice) Less(i, j int) bool { return p[i] < p[j] } 63 func (p Int64Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] } 64 func (p Int64Slice) Sort() { sort.Sort(p) } 65 66 type Int32Slice types.Int32Slice 67 68 func (p Int32Slice) Len() int { return len(p) } 69 func (p Int32Slice) Less(i, j int) bool { return p[i] < p[j] } 70 func (p Int32Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] } 71 func (p Int32Slice) Sort() { sort.Sort(p) } 72 73 type Int16Slice types.Int16Slice 74 75 func (p Int16Slice) Len() int { return len(p) } 76 func (p Int16Slice) Less(i, j int) bool { return p[i] < p[j] } 77 func (p Int16Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] } 78 func (p Int16Slice) Sort() { sort.Sort(p) } 79 80 type Int8Slice types.Int8Slice 81 82 func (p Int8Slice) Len() int { return len(p) } 83 func (p Int8Slice) Less(i, j int) bool { return p[i] < p[j] } 84 func (p Int8Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] } 85 func (p Int8Slice) Sort() { sort.Sort(p) } 86 87 type StringSlice types.StringSlice 88 89 func (p StringSlice) Len() int { return len(p) } 90 func (p StringSlice) Less(i, j int) bool { return p[i] < p[j] } 91 func (p StringSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] } 92 func (p StringSlice) Sort() { sort.Sort(p) } 93 94 type Float64Slice types.Float64Slice 95 96 func (p Float64Slice) Len() int { return len(p) } 97 func (p Float64Slice) Less(i, j int) bool { return p[i] < p[j] || isNaN64(p[i]) && !isNaN64(p[j]) } 98 func (p Float64Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] } 99 func (p Float64Slice) Sort() { sort.Sort(p) } 100 101 type Float32Slice types.Float32Slice 102 103 func (p Float32Slice) Len() int { return len(p) } 104 func (p Float32Slice) Less(i, j int) bool { return p[i] < p[j] || isNaN32(p[i]) && !isNaN32(p[j]) } 105 func (p Float32Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] } 106 func (p Float32Slice) Sort() { sort.Sort(p) } 107 108 type ComparableSlice types.ComparableSlice 109 110 func (p ComparableSlice) Len() int { return len(p) } 111 func (p ComparableSlice) Less(i, j int) bool { return p[i].Compare(p[j]) == types.OrderingLess } 112 func (p ComparableSlice) Swap(i, j int) { p[i], p[j] = p[j], p[i] } 113 func (p ComparableSlice) Sort() { sort.Sort(p) }