Skip to content

Commit d5145de

Browse files
LilithHafnerLilith Hafner
andauthored
fix and test sorting arrays with >1 dimension and custom similar (#49392)
* fix and test sorting arrays with >1 dimension and custom `similar` * add PR number --------- Co-authored-by: Lilith Hafner <[email protected]>
1 parent ca3270b commit d5145de

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

base/sort.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1809,7 +1809,7 @@ function sort!(A::AbstractArray{T};
18091809
by=identity,
18101810
rev::Union{Bool,Nothing}=nothing,
18111811
order::Ordering=Forward, # TODO stop eagerly over-allocating.
1812-
scratch::Union{Vector{T}, Nothing}=similar(A, size(A, dims))) where T
1812+
scratch::Union{Vector{T}, Nothing}=Vector{T}(undef, size(A, dims))) where T
18131813
__sort!(A, Val(dims), maybe_apply_initial_optimizations(alg), ord(lt, by, rev, order), scratch)
18141814
end
18151815
function __sort!(A::AbstractArray{T}, ::Val{K},

test/sorting.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1011,6 +1011,20 @@ end
10111011
end
10121012
end
10131013

1014+
struct MyArray49392{T, N} <: AbstractArray{T, N}
1015+
data::Array{T, N}
1016+
end
1017+
Base.size(A::MyArray49392) = size(A.data)
1018+
Base.getindex(A::MyArray49392, i...) = getindex(A.data, i...)
1019+
Base.setindex!(A::MyArray49392, v, i...) = setindex!(A.data, v, i...)
1020+
Base.similar(A::MyArray49392, ::Type{T}, dims::Dims{N}) where {T, N} = MyArray49392(similar(A.data, T, dims))
1021+
1022+
@testset "Custom matrices (#49392)" begin
1023+
x = rand(10, 10)
1024+
y = MyArray49392(copy(x))
1025+
@test all(sort!(y, dims=2) .== sort!(x,dims=2))
1026+
end
1027+
10141028
# This testset is at the end of the file because it is slow.
10151029
@testset "searchsorted" begin
10161030
numTypes = [ Int8, Int16, Int32, Int64, Int128,

0 commit comments

Comments
 (0)