diff --git a/docs/slice.md b/docs/slice.md index 9a706fd..f82ff79 100644 --- a/docs/slice.md +++ b/docs/slice.md @@ -63,6 +63,10 @@ import ( - [ReplaceAll](#ReplaceAll) - [Repeat](#Repeat) - [Shuffle](#Shuffle) +- [IsAscending](#IsAscending) +- [IsDescending](#IsDescending) +- [IsSorted](#IsSorted) +- [IsSortedByKey](#IsSortedByKey) - [Sort](#Sort) - [SortBy](#SortBy) - [SortByFielddeprecated](#SortByField) @@ -1470,6 +1474,148 @@ func main() { } ``` +### IsAscending + +
Checks if a slice is ascending order.
+ +Signature: + +```go +func IsAscending[T constraints.Ordered](slice []T) bool +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/slice" +) + +func main() { + result1 := slice.IsAscending([]int{1, 2, 3, 4, 5}) + result2 := slice.IsAscending([]int{5, 4, 3, 2, 1}) + result3 := slice.IsAscending([]int{2, 1, 3, 4, 5}) + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // true + // false + // false +} +``` + +### IsDescending + +Checks if a slice is descending order.
+ +Signature: + +```go +func IsDescending[T constraints.Ordered](slice []T) bool +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/slice" +) + +func main() { + result1 := slice.IsDescending([]int{5, 4, 3, 2, 1}) + result2 := slice.IsDescending([]int{1, 2, 3, 4, 5}) + result3 := slice.IsDescending([]int{2, 1, 3, 4, 5}) + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // true + // false + // false +} +``` + +### IsSorted + +Checks if a slice is sorted (ascending or descending).
+ +Signature: + +```go +func IsSorted[T constraints.Ordered](slice []T) bool +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/slice" +) + +func main() { + result1 := slice.IsSorted([]int{5, 4, 3, 2, 1}) + result2 := slice.IsSorted([]int{1, 2, 3, 4, 5}) + result3 := slice.IsSorted([]int{2, 1, 3, 4, 5}) + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // true + // true + // false +} +``` + +### IsSortedByKey + +Checks if a slice is sorted by iteratee function.
+ +Signature: + +```go +func IsSortedByKey[T any, K constraints.Ordered](slice []T, iteratee func(item T) K) bool +``` + +Example: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/slice" +) + +func main() { + result1 := slice.IsSortedByKey([]string{"a", "ab", "abc"}, func(s string) int { + return len(s) + }) + result2 := slice.IsSortedByKey([]string{"abc", "ab", "a"}, func(s string) int { + return len(s) + }) + result3 := slice.IsSortedByKey([]string{"abc", "a", "ab"}, func(s string) int { + return len(s) + }) + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // true + // true + // false +} +``` + ### SortSorts 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`. Ordered type: number(all ints uints floats) or string. diff --git a/docs/slice_zh-CN.md b/docs/slice_zh-CN.md index 5db48e9..1e234c5 100644 --- a/docs/slice_zh-CN.md +++ b/docs/slice_zh-CN.md @@ -63,6 +63,10 @@ import ( - [ReplaceAll](#ReplaceAll) - [Repeat](#Repeat) - [Shuffle](#Shuffle) +- [IsAscending](#IsAscending) +- [IsDescending](#IsDescending) +- [IsSorted](#IsSorted) +- [IsSortedByKey](#IsSortedByKey) - [Sort](#Sort) - [SortBy](#SortBy) - [SortByFielddeprecated](#SortByField) @@ -1472,6 +1476,148 @@ func main() { } ``` +### IsAscending + +
检查切片元素是否按升序排列。
+ +函数签名: + +```go +func IsAscending[T constraints.Ordered](slice []T) bool +``` + +示例: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/slice" +) + +func main() { + result1 := slice.IsAscending([]int{1, 2, 3, 4, 5}) + result2 := slice.IsAscending([]int{5, 4, 3, 2, 1}) + result3 := slice.IsAscending([]int{2, 1, 3, 4, 5}) + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // true + // false + // false +} +``` + +### IsDescending + +检查切片元素是否按降序排列。
+ +函数签名: + +```go +func IsDescending[T constraints.Ordered](slice []T) bool +``` + +示例: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/slice" +) + +func main() { + result1 := slice.IsDescending([]int{5, 4, 3, 2, 1}) + result2 := slice.IsDescending([]int{1, 2, 3, 4, 5}) + result3 := slice.IsDescending([]int{2, 1, 3, 4, 5}) + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // true + // false + // false +} +``` + +### IsSorted + +检查切片元素是否是有序的(升序或降序)。
+ +函数签名: + +```go +func IsSorted[T constraints.Ordered](slice []T) bool +``` + +示例: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/slice" +) + +func main() { + result1 := slice.IsSorted([]int{5, 4, 3, 2, 1}) + result2 := slice.IsSorted([]int{1, 2, 3, 4, 5}) + result3 := slice.IsSorted([]int{2, 1, 3, 4, 5}) + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // true + // true + // false +} +``` + +### IsSortedByKey + +通过iteratee函数,检查切片元素是否是有序的。
+ +函数签名: + +```go +func IsSortedByKey[T any, K constraints.Ordered](slice []T, iteratee func(item T) K) bool +``` + +示例: + +```go +import ( + "fmt" + "github.com/duke-git/lancet/v2/slice" +) + +func main() { + result1 := slice.IsSortedByKey([]string{"a", "ab", "abc"}, func(s string) int { + return len(s) + }) + result2 := slice.IsSortedByKey([]string{"abc", "ab", "a"}, func(s string) int { + return len(s) + }) + result3 := slice.IsSortedByKey([]string{"abc", "a", "ab"}, func(s string) int { + return len(s) + }) + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // true + // true + // false +} +``` + ### Sort对任何有序类型(数字或字符串)的切片进行排序,使用快速排序算法。 默认排序顺序为升序 (asc),如果需要降序,请将参数 `sortOrder` 设置为 `desc`。 Ordered类型:数字(所有整数浮点数)或字符串。