mirror of
https://gitee.com/johng/gf.git
synced 2024-12-05 05:37:55 +08:00
104 lines
2.8 KiB
Go
104 lines
2.8 KiB
Go
package gutil
|
|
|
|
import (
|
|
"github.com/gogf/gf/g/util/gconv"
|
|
"strings"
|
|
)
|
|
|
|
// Comparator is a function that compare a and b, and returns the result as int.
|
|
//
|
|
// Should return a number:
|
|
// negative , if a < b
|
|
// zero , if a == b
|
|
// positive , if a > b
|
|
type Comparator func(a, b interface{}) int
|
|
|
|
// ComparatorString provides a fast comparison on strings.
|
|
func ComparatorString(a, b interface{}) int {
|
|
return strings.Compare(gconv.String(a), gconv.String(b))
|
|
}
|
|
|
|
// ComparatorInt provides a basic comparison on int.
|
|
func ComparatorInt(a, b interface{}) int {
|
|
return gconv.Int(a) - gconv.Int(b)
|
|
}
|
|
|
|
// ComparatorInt8 provides a basic comparison on int8.
|
|
func ComparatorInt8(a, b interface{}) int {
|
|
return int(gconv.Int8(a) - gconv.Int8(b))
|
|
}
|
|
|
|
// ComparatorInt16 provides a basic comparison on int16.
|
|
func ComparatorInt16(a, b interface{}) int {
|
|
return int(gconv.Int16(a) - gconv.Int16(b))
|
|
}
|
|
|
|
// ComparatorInt32 provides a basic comparison on int32.
|
|
func ComparatorInt32(a, b interface{}) int {
|
|
return int(gconv.Int32(a) - gconv.Int32(b))
|
|
}
|
|
|
|
// ComparatorInt64 provides a basic comparison on int64.
|
|
func ComparatorInt64(a, b interface{}) int {
|
|
return int(gconv.Int64(a) - gconv.Int64(b))
|
|
}
|
|
|
|
// ComparatorUint provides a basic comparison on uint.
|
|
func ComparatorUint(a, b interface{}) int {
|
|
return int(gconv.Uint(a) - gconv.Uint(b))
|
|
}
|
|
|
|
// ComparatorUint8 provides a basic comparison on uint8.
|
|
func ComparatorUint8(a, b interface{}) int {
|
|
return int(gconv.Uint8(a) - gconv.Uint8(b))
|
|
}
|
|
|
|
// ComparatorUint16 provides a basic comparison on uint16.
|
|
func ComparatorUint16(a, b interface{}) int {
|
|
return int(gconv.Uint16(a) - gconv.Uint16(b))
|
|
}
|
|
|
|
// ComparatorUint32 provides a basic comparison on uint32.
|
|
func ComparatorUint32(a, b interface{}) int {
|
|
return int(gconv.Uint32(a) - gconv.Uint32(b))
|
|
}
|
|
|
|
// ComparatorUint64 provides a basic comparison on uint64.
|
|
func ComparatorUint64(a, b interface{}) int {
|
|
return int(gconv.Uint64(a) - gconv.Uint64(b))
|
|
}
|
|
|
|
// ComparatorFloat32 provides a basic comparison on float32.
|
|
func ComparatorFloat32(a, b interface{}) int {
|
|
return int(gconv.Float32(a) - gconv.Float32(b))
|
|
}
|
|
|
|
// ComparatorFloat64 provides a basic comparison on float64.
|
|
func ComparatorFloat64(a, b interface{}) int {
|
|
return int(gconv.Float64(a) - gconv.Float64(b))
|
|
}
|
|
|
|
// ComparatorByte provides a basic comparison on byte.
|
|
func ComparatorByte(a, b interface{}) int {
|
|
return int(gconv.Byte(a) - gconv.Byte(b))
|
|
}
|
|
|
|
// ComparatorRune provides a basic comparison on rune.
|
|
func ComparatorRune(a, b interface{}) int {
|
|
return int(gconv.Rune(a) - gconv.Rune(b))
|
|
}
|
|
|
|
// ComparatorTime provides a basic comparison on time.Time.
|
|
func ComparatorTime(a, b interface{}) int {
|
|
aTime := gconv.Time(a)
|
|
bTime := gconv.Time(b)
|
|
switch {
|
|
case aTime.After(bTime):
|
|
return 1
|
|
case aTime.Before(bTime):
|
|
return -1
|
|
default:
|
|
return 0
|
|
}
|
|
}
|