2019-05-14 16:13:59 +00:00
|
|
|
package models
|
|
|
|
|
2021-08-11 14:52:47 +00:00
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
2019-05-14 16:13:59 +00:00
|
|
|
|
|
|
|
type Order int
|
|
|
|
|
|
|
|
const (
|
|
|
|
Asc Order = iota
|
|
|
|
Desc
|
|
|
|
)
|
|
|
|
|
|
|
|
func IntSort(a, b int, o Order) bool {
|
|
|
|
if o == Asc {
|
|
|
|
return a < b
|
|
|
|
}
|
|
|
|
return a > b
|
|
|
|
}
|
|
|
|
|
|
|
|
func Int32Sort(a, b int32, o Order) bool {
|
|
|
|
if o == Asc {
|
|
|
|
return a < b
|
|
|
|
}
|
|
|
|
return a > b
|
|
|
|
}
|
|
|
|
|
|
|
|
func Int64Sort(a, b int64, o Order) bool {
|
|
|
|
if o == Asc {
|
|
|
|
return a < b
|
|
|
|
}
|
|
|
|
return a > b
|
|
|
|
}
|
|
|
|
|
2022-01-29 13:00:32 +00:00
|
|
|
func Float64Sort(a, b float64, o Order) bool {
|
|
|
|
if o == Asc {
|
|
|
|
return a < b
|
|
|
|
}
|
|
|
|
return a > b
|
|
|
|
}
|
|
|
|
|
2021-08-11 14:52:47 +00:00
|
|
|
func UInt64Sort(a, b uint64, o Order) bool {
|
|
|
|
if o == Asc {
|
|
|
|
return a < b
|
|
|
|
}
|
|
|
|
return a > b
|
|
|
|
}
|
|
|
|
|
2019-05-14 16:13:59 +00:00
|
|
|
func DateSort(a, b *time.Time, o Order) bool {
|
|
|
|
if o == Desc {
|
|
|
|
if a == nil || b == nil {
|
|
|
|
return b == nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return a.After(*b)
|
|
|
|
}
|
|
|
|
|
|
|
|
if a == nil || b == nil {
|
|
|
|
return a == nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return a.Before(*b)
|
|
|
|
}
|
2021-08-11 14:52:47 +00:00
|
|
|
|
|
|
|
func StringSort(a, b string, o Order) bool {
|
|
|
|
result := strings.Compare(a, b)
|
|
|
|
if o == Asc {
|
|
|
|
return result < 0
|
|
|
|
}
|
|
|
|
return result > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func BoolSort(a, b bool, o Order) bool {
|
|
|
|
if o == Asc {
|
|
|
|
return !a && b
|
|
|
|
}
|
|
|
|
return a && !b
|
|
|
|
}
|