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

fix sorting for iterables that define copymutable #52086

Merged
merged 1 commit into from
Nov 9, 2023
Merged
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
4 changes: 2 additions & 2 deletions base/sort.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1500,7 +1500,7 @@ function sort(v; kws...)
size = IteratorSize(v)
size == HasShape{0}() && throw(ArgumentError("$v cannot be sorted"))
size == IsInfinite() && throw(ArgumentError("infinite iterator $v cannot be sorted"))
sort!(copymutable(v); kws...)
sort!(collect(v); kws...)
end
sort(v::AbstractVector; kws...) = sort!(copymutable(v); kws...) # for method disambiguation
sort(::AbstractString; kws...) =
Expand All @@ -1512,7 +1512,7 @@ function sort(x::NTuple{N}; lt::Function=isless, by::Function=identity,
rev::Union{Bool,Nothing}=nothing, order::Ordering=Forward) where N
o = ord(lt,by,rev,order)
if N > 9
v = sort!(copymutable(x), DEFAULT_STABLE, o)
v = sort!(collect(x), DEFAULT_STABLE, o)
tuple((v[i] for i in 1:N)...)
else
_sort(x, o)
Expand Down
5 changes: 5 additions & 0 deletions test/sorting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,11 @@ end

@test_throws ArgumentError sort("string")
@test_throws ArgumentError("1 cannot be sorted") sort(1)

@test sort(Set((1, 3, 6))) == [1, 3, 6]
@test sort(Dict((1=>9, 3=>2, 6=>5))) == [1=>9, 3=>2, 6=>5]
@test sort(keys(Dict((1=>2, 3=>5, 6=>9)))) == [1, 3, 6]
@test sort(values(Dict((1=>9, 3=>2, 6=>5)))) == [2, 5, 9]
end

@testset "sort!(::AbstractVector{<:Integer}) with short int range" begin
Expand Down