From a44c89504b1952132f6441504c42f4c9857d4a40 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 21 Sep 2022 08:04:57 +0530 Subject: [PATCH] More convenient interface for sorting --- tools/utils/misc.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tools/utils/misc.go b/tools/utils/misc.go index 30a470d28..ee639eb12 100644 --- a/tools/utils/misc.go +++ b/tools/utils/misc.go @@ -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]) }) +}