diff --git a/base/sort.jl b/base/sort.jl index b78f773ad9f72..99f2ed3e1aeb8 100644 --- a/base/sort.jl +++ b/base/sort.jl @@ -1809,7 +1809,7 @@ function sort!(A::AbstractArray{T}; by=identity, rev::Union{Bool,Nothing}=nothing, order::Ordering=Forward, # TODO stop eagerly over-allocating. - scratch::Union{Vector{T}, Nothing}=similar(A, size(A, dims))) where T + scratch::Union{Vector{T}, Nothing}=Vector{T}(undef, size(A, dims))) where T __sort!(A, Val(dims), maybe_apply_initial_optimizations(alg), ord(lt, by, rev, order), scratch) end function __sort!(A::AbstractArray{T}, ::Val{K}, diff --git a/test/sorting.jl b/test/sorting.jl index 0528d9d81c296..cf98182307088 100644 --- a/test/sorting.jl +++ b/test/sorting.jl @@ -1011,6 +1011,20 @@ end end end +struct MyArray49392{T, N} <: AbstractArray{T, N} + data::Array{T, N} +end +Base.size(A::MyArray49392) = size(A.data) +Base.getindex(A::MyArray49392, i...) = getindex(A.data, i...) +Base.setindex!(A::MyArray49392, v, i...) = setindex!(A.data, v, i...) +Base.similar(A::MyArray49392, ::Type{T}, dims::Dims{N}) where {T, N} = MyArray49392(similar(A.data, T, dims)) + +@testset "Custom matrices (#49392)" begin + x = rand(10, 10) + y = MyArray49392(copy(x)) + @test all(sort!(y, dims=2) .== sort!(x,dims=2)) +end + # This testset is at the end of the file because it is slow. @testset "searchsorted" begin numTypes = [ Int8, Int16, Int32, Int64, Int128,