mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-13 17:22:27 +08:00
feat: add IsSorted
This commit is contained in:
@@ -842,9 +842,9 @@ func IsAscending[T constraints.Ordered](slice []T) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
// Isdescending checks if a slice is descending order.
|
// IsDescending checks if a slice is descending order.
|
||||||
// Play: todo
|
// 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++ {
|
for i := 1; i < len(slice); i++ {
|
||||||
if slice[i-1] < slice[i] {
|
if slice[i-1] < slice[i] {
|
||||||
return false
|
return false
|
||||||
@@ -854,6 +854,12 @@ func Isdescending[T constraints.Ordered](slice []T) bool {
|
|||||||
return true
|
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.
|
// 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`.
|
// default sort order is ascending (asc), if want descending order, set param `sortOrder` to `desc`.
|
||||||
// Play: https://go.dev/play/p/V9AVjzf_4Fk
|
// Play: https://go.dev/play/p/V9AVjzf_4Fk
|
||||||
|
|||||||
@@ -701,11 +701,11 @@ func ExampleIsAscending() {
|
|||||||
// false
|
// false
|
||||||
}
|
}
|
||||||
|
|
||||||
func ExampleIsdescending() {
|
func ExampleIsDescending() {
|
||||||
|
|
||||||
result1 := Isdescending([]int{5, 4, 3, 2, 1})
|
result1 := IsDescending([]int{5, 4, 3, 2, 1})
|
||||||
result2 := Isdescending([]int{1, 2, 3, 4, 5})
|
result2 := IsDescending([]int{1, 2, 3, 4, 5})
|
||||||
result3 := Isdescending([]int{2, 1, 3, 4, 5})
|
result3 := IsDescending([]int{2, 1, 3, 4, 5})
|
||||||
|
|
||||||
fmt.Println(result1)
|
fmt.Println(result1)
|
||||||
fmt.Println(result2)
|
fmt.Println(result2)
|
||||||
@@ -717,6 +717,22 @@ func ExampleIsdescending() {
|
|||||||
// false
|
// false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ExampleIsSorted() {
|
||||||
|
|
||||||
|
result1 := IsSorted([]int{1, 2, 3, 4, 5})
|
||||||
|
result2 := IsSorted([]int{5, 4, 3, 2, 1})
|
||||||
|
result3 := IsSorted([]int{2, 1, 3, 4, 5})
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
// true
|
||||||
|
// false
|
||||||
|
}
|
||||||
|
|
||||||
func ExampleSort() {
|
func ExampleSort() {
|
||||||
nums := []int{1, 4, 3, 2, 5}
|
nums := []int{1, 4, 3, 2, 5}
|
||||||
|
|
||||||
|
|||||||
@@ -611,12 +611,20 @@ func TestIsAscending(t *testing.T) {
|
|||||||
assert.Equal(false, IsAscending([]int{2, 1, 3, 4, 5}))
|
assert.Equal(false, IsAscending([]int{2, 1, 3, 4, 5}))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestIsdescending(t *testing.T) {
|
func TestIsDescending(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestIsdescending")
|
assert := internal.NewAssert(t, "TestIsDescending")
|
||||||
|
|
||||||
assert.Equal(true, Isdescending([]int{5, 4, 3, 2, 1}))
|
assert.Equal(true, IsDescending([]int{5, 4, 3, 2, 1}))
|
||||||
assert.Equal(false, Isdescending([]int{1, 2, 3, 4, 5}))
|
assert.Equal(false, IsDescending([]int{1, 2, 3, 4, 5}))
|
||||||
assert.Equal(false, Isdescending([]int{2, 1, 3, 4, 5}))
|
assert.Equal(false, IsDescending([]int{2, 1, 3, 4, 5}))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestIsSorted(t *testing.T) {
|
||||||
|
assert := internal.NewAssert(t, "TestIsSorted")
|
||||||
|
|
||||||
|
assert.Equal(true, IsSorted([]int{5, 4, 3, 2, 1}))
|
||||||
|
assert.Equal(true, IsSorted([]int{1, 2, 3, 4, 5}))
|
||||||
|
assert.Equal(false, IsSorted([]int{2, 1, 3, 4, 5}))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSort(t *testing.T) {
|
func TestSort(t *testing.T) {
|
||||||
|
|||||||
Reference in New Issue
Block a user