Skip to content

Commit

Permalink
fix and test sorting arrays with >1 dimension and custom similar (#…
Browse files Browse the repository at this point in the history
…49392)

* fix and test sorting arrays with >1 dimension and custom `similar`

* add PR number

---------

Co-authored-by: Lilith Hafner <Lilith.Hafner@gmail.com>
  • Loading branch information
LilithHafner and Lilith Hafner authored May 31, 2023
1 parent ca3270b commit d5145de
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
2 changes: 1 addition & 1 deletion base/sort.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
14 changes: 14 additions & 0 deletions test/sorting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit d5145de

Please sign in to comment.