github.com/zhongdalu/gf@v1.0.0/g/util/gutil/gutil_comparator.go (about) 1 package gutil 2 3 import ( 4 "github.com/zhongdalu/gf/g/util/gconv" 5 "strings" 6 ) 7 8 // Comparator is a function that compare a and b, and returns the result as int. 9 // 10 // Should return a number: 11 // negative , if a < b 12 // zero , if a == b 13 // positive , if a > b 14 type Comparator func(a, b interface{}) int 15 16 // ComparatorString provides a fast comparison on strings. 17 func ComparatorString(a, b interface{}) int { 18 return strings.Compare(gconv.String(a), gconv.String(b)) 19 } 20 21 // ComparatorInt provides a basic comparison on int. 22 func ComparatorInt(a, b interface{}) int { 23 return gconv.Int(a) - gconv.Int(b) 24 } 25 26 // ComparatorInt8 provides a basic comparison on int8. 27 func ComparatorInt8(a, b interface{}) int { 28 return int(gconv.Int8(a) - gconv.Int8(b)) 29 } 30 31 // ComparatorInt16 provides a basic comparison on int16. 32 func ComparatorInt16(a, b interface{}) int { 33 return int(gconv.Int16(a) - gconv.Int16(b)) 34 } 35 36 // ComparatorInt32 provides a basic comparison on int32. 37 func ComparatorInt32(a, b interface{}) int { 38 return int(gconv.Int32(a) - gconv.Int32(b)) 39 } 40 41 // ComparatorInt64 provides a basic comparison on int64. 42 func ComparatorInt64(a, b interface{}) int { 43 return int(gconv.Int64(a) - gconv.Int64(b)) 44 } 45 46 // ComparatorUint provides a basic comparison on uint. 47 func ComparatorUint(a, b interface{}) int { 48 return int(gconv.Uint(a) - gconv.Uint(b)) 49 } 50 51 // ComparatorUint8 provides a basic comparison on uint8. 52 func ComparatorUint8(a, b interface{}) int { 53 return int(gconv.Uint8(a) - gconv.Uint8(b)) 54 } 55 56 // ComparatorUint16 provides a basic comparison on uint16. 57 func ComparatorUint16(a, b interface{}) int { 58 return int(gconv.Uint16(a) - gconv.Uint16(b)) 59 } 60 61 // ComparatorUint32 provides a basic comparison on uint32. 62 func ComparatorUint32(a, b interface{}) int { 63 return int(gconv.Uint32(a) - gconv.Uint32(b)) 64 } 65 66 // ComparatorUint64 provides a basic comparison on uint64. 67 func ComparatorUint64(a, b interface{}) int { 68 return int(gconv.Uint64(a) - gconv.Uint64(b)) 69 } 70 71 // ComparatorFloat32 provides a basic comparison on float32. 72 func ComparatorFloat32(a, b interface{}) int { 73 return int(gconv.Float32(a) - gconv.Float32(b)) 74 } 75 76 // ComparatorFloat64 provides a basic comparison on float64. 77 func ComparatorFloat64(a, b interface{}) int { 78 return int(gconv.Float64(a) - gconv.Float64(b)) 79 } 80 81 // ComparatorByte provides a basic comparison on byte. 82 func ComparatorByte(a, b interface{}) int { 83 return int(gconv.Byte(a) - gconv.Byte(b)) 84 } 85 86 // ComparatorRune provides a basic comparison on rune. 87 func ComparatorRune(a, b interface{}) int { 88 return int(gconv.Rune(a) - gconv.Rune(b)) 89 } 90 91 // ComparatorTime provides a basic comparison on time.Time. 92 func ComparatorTime(a, b interface{}) int { 93 aTime := gconv.Time(a) 94 bTime := gconv.Time(b) 95 switch { 96 case aTime.After(bTime): 97 return 1 98 case aTime.Before(bTime): 99 return -1 100 default: 101 return 0 102 } 103 }