-
-
Notifications
You must be signed in to change notification settings - Fork 843
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add ToSorted function to return a sorted copy of a slice #413
base: master
Are you sure you want to change the base?
Conversation
I'm wondering why it hasn't merged or discusses even. The current plain go implementation requires a separate sort package interfaces. related |
// ToSorted return a sorted copy of a slice | ||
// Play: https://go.dev/play/p/G4-aR2Yxh3M | ||
func ToSorted[T constraints.Ordered](collection []T) []T { | ||
if collection == nil { | ||
return nil | ||
} | ||
|
||
copied := make([]T, len(collection)) | ||
copy(copied, collection) | ||
slices.Sort(copied) | ||
return copied | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would have used "Sort"
// ToSorted return a sorted copy of a slice | |
// Play: https://go.dev/play/p/G4-aR2Yxh3M | |
func ToSorted[T constraints.Ordered](collection []T) []T { | |
if collection == nil { | |
return nil | |
} | |
copied := make([]T, len(collection)) | |
copy(copied, collection) | |
slices.Sort(copied) | |
return copied | |
} | |
// Sort return a sorted copy of a slice | |
// Play: https://go.dev/play/p/G4-aR2Yxh3M | |
func Sort[T constraints.Ordered](collection []T) []T { | |
if collection == nil { | |
return nil | |
} | |
copied := make([]T, len(collection)) | |
copy(copied, collection) | |
slices.Sort(copied) | |
return copied | |
} |
@@ -116,6 +116,7 @@ Supported helpers for slices: | |||
- [Compact](#compact) | |||
- [IsSorted](#issorted) | |||
- [IsSortedByKey](#issortedbykey) | |||
- [ToSorted](#tosorted) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- [ToSorted](#tosorted) | |
- [Sort](#sort) |
### ToSorted | ||
|
||
Return a sorted copy of a slice. | ||
|
||
```go | ||
sorted := lo.ToSorted([]int{0, 1, 4, 3, 2}) | ||
// [0 1 2 3 4] | ||
``` | ||
|
||
[[play](https://go.dev/play/p/G4-aR2Yxh3M)] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
### ToSorted | |
Return a sorted copy of a slice. | |
```go | |
sorted := lo.ToSorted([]int{0, 1, 4, 3, 2}) | |
// [0 1 2 3 4] | |
``` | |
[[play](https://go.dev/play/p/G4-aR2Yxh3M)] | |
### Sort | |
Return a sorted copy of a slice. | |
```go | |
sorted := lo.Sort([]int{0, 1, 4, 3, 2}) | |
// [0 1 2 3 4] | |
``` | |
[[play](https://go.dev/play/p/G4-aR2Yxh3M)] |
Add a function to return a sorted copy of a slice. Tests also added.
Feel free to comment.