More convenient interface for sorting

This commit is contained in:
Kovid Goyal
2022-09-21 08:04:57 +05:30
parent 8807f6d539
commit a44c89504b

View File

@@ -4,6 +4,7 @@ package utils
import (
"fmt"
"sort"
)
var _ = fmt.Print
@@ -21,3 +22,11 @@ func Reversed[T any](s []T) []T {
}
return ans
}
func Sort[T any](s []T, less func(a, b T) bool) {
sort.Slice(s, func(i, j int) bool { return less(s[i], s[j]) })
}
func StableSort[T any](s []T, less func(a, b T) bool) {
sort.SliceStable(s, func(i, j int) bool { return less(s[i], s[j]) })
}