Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Expand Up @@ -116,6 +116,7 @@ Supported helpers for slices:
- [Compact](#compact)
- [IsSorted](#issorted)
- [IsSortedByKey](#issortedbykey)
- [ToSorted](#tosorted)

Supported helpers for maps:

Expand Down Expand Up @@ -924,6 +925,17 @@ slice := lo.IsSortedByKey([]string{"a", "bb", "ccc"}, func(s string) int {

[[play](https://go.dev/play/p/wiG6XyBBu49)]

### 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)]

### Keys

Creates an array of the map keys.
Expand Down
14 changes: 14 additions & 0 deletions slice.go
Expand Up @@ -4,6 +4,7 @@ import (
"math/rand"

"golang.org/x/exp/constraints"
"golang.org/x/exp/slices"
)

// Filter iterates over elements of collection, returning an array of all elements predicate returns truthy for.
Expand Down Expand Up @@ -592,3 +593,16 @@ func IsSortedByKey[T any, K constraints.Ordered](collection []T, iteratee func(i

return true
}

// 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
}
19 changes: 19 additions & 0 deletions slice_benchmark_test.go
Expand Up @@ -171,3 +171,22 @@ func BenchmarkReplace(b *testing.B) {
})
}
}

func BenchmarkToSorted(b *testing.B) {
lengths := []int{1_000, 10_000, 100_000}
for _, n := range lengths {
strs := genSliceString(n)
b.Run(fmt.Sprintf("strings_%d", n), func(b *testing.B) {
_ = ToSorted(strs)
})
}

for _, n := range lengths {
ints := genSliceInt(n)
b.Run(fmt.Sprintf("ints%d", n), func(b *testing.B) {
for i := 0; i < b.N; i++ {
_ = ToSorted(ints)
}
})
}
}
10 changes: 10 additions & 0 deletions slice_example_test.go
Expand Up @@ -456,3 +456,13 @@ func ExampleIsSortedByKey() {

// Output: true
}

func ExampleToSorted() {
list := []int{0, 1, 4, 3, 2}

result := ToSorted(list)

fmt.Printf("%v", result)

// Output: [0 1 2 3 4]
}
23 changes: 21 additions & 2 deletions slice_test.go
Expand Up @@ -388,7 +388,7 @@ func TestAssociate(t *testing.T) {

func TestSliceToMap(t *testing.T) {
t.Parallel()

type foo struct {
baz string
bar int
Expand Down Expand Up @@ -626,7 +626,7 @@ func TestSlice(t *testing.T) {
out16 := Slice(in, -10, 1)
out17 := Slice(in, -1, 3)
out18 := Slice(in, -10, 7)

is.Equal([]int{}, out1)
is.Equal([]int{0}, out2)
is.Equal([]int{0, 1, 2, 3, 4}, out3)
Expand Down Expand Up @@ -759,3 +759,22 @@ func TestIsSortedByKey(t *testing.T) {
return ret
}))
}

func TestToSorted(t *testing.T) {
t.Parallel()
is := assert.New(t)

s1 := []int{1, 2, 5, 3, 2}
s2 := []int{1, 2, 2, 3, 5}

r1 := ToSorted(s1)
is.Equal(s2, r1)
is.NotEqual(&s1, &r1)

s3 := []string{"a", "c", "b", "e", "d"}
s4 := []string{"a", "b", "c", "d", "e"}

r2 := ToSorted(s3)
is.Equal(s4, r2)
is.NotEqual(&s3, &r2)
}