github.com/gogf/gf@v1.16.9/util/gutil/gutil_comparator.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/gogf/gf. 6 7 package gutil 8 9 import ( 10 "strings" 11 12 "github.com/gogf/gf/util/gconv" 13 ) 14 15 // Comparator is a function that compare a and b, and returns the result as int. 16 // 17 // Should return a number: 18 // negative , if a < b 19 // zero , if a == b 20 // positive , if a > b 21 type Comparator func(a, b interface{}) int 22 23 // ComparatorString provides a fast comparison on strings. 24 func ComparatorString(a, b interface{}) int { 25 return strings.Compare(gconv.String(a), gconv.String(b)) 26 } 27 28 // ComparatorInt provides a basic comparison on int. 29 func ComparatorInt(a, b interface{}) int { 30 return gconv.Int(a) - gconv.Int(b) 31 } 32 33 // ComparatorInt8 provides a basic comparison on int8. 34 func ComparatorInt8(a, b interface{}) int { 35 return int(gconv.Int8(a) - gconv.Int8(b)) 36 } 37 38 // ComparatorInt16 provides a basic comparison on int16. 39 func ComparatorInt16(a, b interface{}) int { 40 return int(gconv.Int16(a) - gconv.Int16(b)) 41 } 42 43 // ComparatorInt32 provides a basic comparison on int32. 44 func ComparatorInt32(a, b interface{}) int { 45 return int(gconv.Int32(a) - gconv.Int32(b)) 46 } 47 48 // ComparatorInt64 provides a basic comparison on int64. 49 func ComparatorInt64(a, b interface{}) int { 50 return int(gconv.Int64(a) - gconv.Int64(b)) 51 } 52 53 // ComparatorUint provides a basic comparison on uint. 54 func ComparatorUint(a, b interface{}) int { 55 return int(gconv.Uint(a) - gconv.Uint(b)) 56 } 57 58 // ComparatorUint8 provides a basic comparison on uint8. 59 func ComparatorUint8(a, b interface{}) int { 60 return int(gconv.Uint8(a) - gconv.Uint8(b)) 61 } 62 63 // ComparatorUint16 provides a basic comparison on uint16. 64 func ComparatorUint16(a, b interface{}) int { 65 return int(gconv.Uint16(a) - gconv.Uint16(b)) 66 } 67 68 // ComparatorUint32 provides a basic comparison on uint32. 69 func ComparatorUint32(a, b interface{}) int { 70 return int(gconv.Uint32(a) - gconv.Uint32(b)) 71 } 72 73 // ComparatorUint64 provides a basic comparison on uint64. 74 func ComparatorUint64(a, b interface{}) int { 75 return int(gconv.Uint64(a) - gconv.Uint64(b)) 76 } 77 78 // ComparatorFloat32 provides a basic comparison on float32. 79 func ComparatorFloat32(a, b interface{}) int { 80 aFloat := gconv.Float32(a) 81 bFloat := gconv.Float32(b) 82 if aFloat == bFloat { 83 return 0 84 } 85 if aFloat > bFloat { 86 return 1 87 } 88 return -1 89 } 90 91 // ComparatorFloat64 provides a basic comparison on float64. 92 func ComparatorFloat64(a, b interface{}) int { 93 aFloat := gconv.Float64(a) 94 bFloat := gconv.Float64(b) 95 if aFloat == bFloat { 96 return 0 97 } 98 if aFloat > bFloat { 99 return 1 100 } 101 return -1 102 } 103 104 // ComparatorByte provides a basic comparison on byte. 105 func ComparatorByte(a, b interface{}) int { 106 return int(gconv.Byte(a) - gconv.Byte(b)) 107 } 108 109 // ComparatorRune provides a basic comparison on rune. 110 func ComparatorRune(a, b interface{}) int { 111 return int(gconv.Rune(a) - gconv.Rune(b)) 112 } 113 114 // ComparatorTime provides a basic comparison on time.Time. 115 func ComparatorTime(a, b interface{}) int { 116 aTime := gconv.Time(a) 117 bTime := gconv.Time(b) 118 switch { 119 case aTime.After(bTime): 120 return 1 121 case aTime.Before(bTime): 122 return -1 123 default: 124 return 0 125 } 126 }