feat: add IsSorted

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

No files matched your search

+8 -2
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