1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-09 23:22:28 +08:00

feat: add IsSorted

This commit is contained in:
dudaodong
2023-02-09 19:30:11 +08:00
parent abf392117a
commit a7e77fa98d
3 changed files with 41 additions and 11 deletions

View File

@@ -842,9 +842,9 @@ func IsAscending[T constraints.Ordered](slice []T) bool {
return true
}
// Isdescending checks if a slice is descending order.
// IsDescending checks if a slice is descending order.
// Play: todo
func Isdescending[T constraints.Ordered](slice []T) bool {
func IsDescending[T constraints.Ordered](slice []T) bool {
for i := 1; i < len(slice); i++ {
if slice[i-1] < slice[i] {
return false
@@ -854,6 +854,12 @@ func Isdescending[T constraints.Ordered](slice []T) bool {
return true
}
// IsSorted checks if a slice is sorted(ascending or descending).
// Play: todo
func IsSorted[T constraints.Ordered](slice []T) bool {
return IsAscending(slice) || IsDescending(slice)
}
// Sort sorts a slice of any ordered type(number or string), use quick sort algrithm.
// default sort order is ascending (asc), if want descending order, set param `sortOrder` to `desc`.
// Play: https://go.dev/play/p/V9AVjzf_4Fk