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

RFC: Add a fallback method for sort, plus one for strings #16853

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions base/sort.jl
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,13 @@ end
sort(v::AbstractVector; kws...) = sort!(copymutable(v); kws...)


## other iterables and fallback ##

sort(s::String; kws...) = String(sort(collect(s); kws...))
sort(n::Number) = throw(MethodError(sort, n))
sort(itr; kws...) = sort(collect(itr); kws...)


## selectperm: the permutation to sort the first k elements of an array ##

selectperm(v::AbstractVector, k::Union{Integer,OrdinalRange}; kwargs...) =
Expand Down
8 changes: 8 additions & 0 deletions test/sorting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -348,3 +348,11 @@ end

# issue #12833 - type stability of sort
@test Base.return_types(sort, (Vector{Int},)) == [Vector{Int}]

# general iterables
@test sort(take([3,2,1], 2)) == [2,3]
@test sort(drop([1,3,2], 1)) == [2,3]
@test sort("Julia") == "Jailu"
@test sort("Julia", by=isupper) == "uliaJ"
@test sort(graphemes("bca")) == collect(graphemes("abc"))
@test_throws MethodError sort(1)