-
Notifications
You must be signed in to change notification settings - Fork 0
/
less.go
35 lines (29 loc) · 852 Bytes
/
less.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package slice
import (
"sort"
"golang.org/x/exp/constraints"
)
// Less is used for sorting. Less does not inheirently mean "less than", only
// that when sorting the index should be less. For instance, when sorting from
// highest to lowest, the index of the greater value will be less that the value
// it is compared against.
type Less[T any] func(i, j T) bool
// Sort the slice using the Less function.
func (l Less[T]) Sort(s []T) []T {
sort.Slice(s, func(i, j int) bool {
return l(s[i], s[j])
})
return s
}
// LT returns an instance of Less[T] that does a less than comparison.
func LT[T constraints.Ordered]() Less[T] {
return func(i, j T) bool {
return i < j
}
}
// LT returns an instance of Less[T] that does a greater than comparison.
func GT[T constraints.Ordered]() Less[T] {
return func(i, j T) bool {
return i > j
}
}