-
Notifications
You must be signed in to change notification settings - Fork 0
/
sort.go
36 lines (30 loc) · 1006 Bytes
/
sort.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
36
//go:build go1.21
package slices2
import (
"cmp"
"slices"
)
// SortInPlace sorts an input slice s of any ordered type in ascending order (in place).
// This sort is not guaranteed to be stable.
func SortInPlace[S ~[]E, E cmp.Ordered](s S) {
slices.Sort(s)
}
// SortFuncInPlace sorts input slice s of any type in ascending order using the cmp function.
// This sort is not guaranteed to be stable.
func SortFuncInPlace[S ~[]E, E any](s S, cmp func(E, E) int) {
slices.SortFunc(s, cmp)
}
// Sorted returns new slice with elements of input slice s sorted.
// As opposite to [SortInPlace] this function always allocates new array.
func Sorted[S ~[]E, E cmp.Ordered](s S) S {
s = Clone(s)
SortInPlace(s)
return s
}
// SortedFunc returns new slice with elements of input slice s sorted using the cmp function.
// As opposite to [SortFuncInPlace] this function always allocates new array.
func SortedFunc[S ~[]E, E any](s S, cmp func(E, E) int) S {
s = Clone(s)
SortFuncInPlace(s, cmp)
return s
}