1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-09 15:12:26 +08:00

feat: add SortBy function for slice

This commit is contained in:
dudaodong
2022-12-02 14:53:57 +08:00
parent ec27ad4c40
commit 280ecb5cef
3 changed files with 65 additions and 3 deletions

View File

@@ -725,6 +725,12 @@ func Sort[T lancetconstraints.Ordered](slice []T, sortOrder ...string) {
}
}
// SortBy sorts the slice in ascending order as determined by the less function.
// This sort is not guaranteed to be stable
func SortBy[T any](slice []T, less func(a, b T) bool) {
quickSortBy(slice, 0, len(slice)-1, less)
}
// SortByField return sorted slice by field
// slice element should be struct, field type should be int, uint, string, or bool
// default sortType is ascending (asc), if descending order, set sortType to desc