mirror of
https://gitee.com/johng/gf.git
synced 2024-12-03 04:37:49 +08:00
111 lines
3.0 KiB
Go
111 lines
3.0 KiB
Go
// Copyright 2019 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
|
//
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
// If a copy of the MIT was not distributed with this file,
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
package gutil
|
|
|
|
import (
|
|
"strings"
|
|
|
|
"github.com/gogf/gf/util/gconv"
|
|
)
|
|
|
|
// 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
|
|
}
|
|
}
|