diff --git a/docs/api/packages/slice.md b/docs/api/packages/slice.md index 3037c6e..0f31936 100644 --- a/docs/api/packages/slice.md +++ b/docs/api/packages/slice.md @@ -77,7 +77,7 @@ import ( - [IsSortedByKey](#IsSortedByKey) - [Sort](#Sort) - [SortBy](#SortBy) -- [SortByFielddeprecated](#SortByField) +- [SortByField](#SortByField) - [Some](#Some) - [StringSlicedeprecated](#StringSlice) - [SymmetricDifference](#SymmetricDifference) @@ -2002,9 +2002,9 @@ func main() { } ``` -### SortByField (已弃用: 请使用 Sort 或 SortBy 代替该方法) +### SortByField -
按字段对结构切片进行排序。slice元素应为struct,字段类型应为int、uint、string或bool。 默认排序类型是升序(asc),如果是降序,设置 sortType 为 desc
+按字段对结构体切片进行排序。slice元素应为struct,排序字段field类型应为int、uint、string或bool。 默认排序类型是升序(asc),如果是降序,设置 sortType 为 desc
函数签名: diff --git a/docs/en/api/packages/slice.md b/docs/en/api/packages/slice.md index 5060126..b62479c 100644 --- a/docs/en/api/packages/slice.md +++ b/docs/en/api/packages/slice.md @@ -77,7 +77,7 @@ import ( - [IsSortedByKey](#IsSortedByKey) - [Sort](#Sort) - [SortBy](#SortBy) -- [SortByFielddeprecated](#SortByField) +- [SortByField](#SortByField) - [Some](#Some) - [StringSlicedeprecated](#StringSlice) - [SymmetricDifference](#SymmetricDifference) @@ -2000,9 +2000,9 @@ func main() { } ``` -### SortByField (Deprecated: use Sort and SortBy for replacement) +### SortByField -Sort struct slice by field. Slice element should be struct, field type should be int, uint, string, or bool. Default sort type is ascending (asc), if descending order, set sortType to desc.
+Sort struct slice by field. Slice element should be struct, `field` param type should be int, uint, string, or bool. Default sort type is ascending (asc), if descending order, set `sortType` param to desc.
Signature: diff --git a/slice/slice.go b/slice/slice.go index f1fe871..6a1dc47 100644 --- a/slice/slice.go +++ b/slice/slice.go @@ -1008,7 +1008,7 @@ func SortBy[T any](slice []T, less func(a, b T) bool) { // default sortType is ascending (asc), if descending order, set sortType to desc // This function is deprecated, use Sort and SortBy for replacement. // Play: https://go.dev/play/p/fU1prOBP9p1 -func SortByField(slice any, field string, sortType ...string) error { +func SortByField[T any](slice []T, field string, sortType ...string) error { sv := sliceValue(slice) t := sv.Type().Elem()