Skip to content

Sorting custom AbstractArray has type instability #49394

@LilithHafner

Description

@LilithHafner

Sorting custom arrays produces an output whose type depends on the dimension over which the array is sorted.
This only happens with sort (not sort!), and AbstractArrays that are neither AbstractVectors nor Arrays.
It was introduced by #12823 in 2015.

SparseMatrixCSC is one of an unbounded set of examples where this could come up.

julia> using SparseArrays

julia> sort(sprand(5, 5, .1), dims=1)
5×5 reshape(::SparseVector{Float64, Int64}, 5, 5) with eltype Float64:
 0.0       0.0  0.0       0.0  0.0
 0.0       0.0  0.0       0.0  0.0
 0.0       0.0  0.0       0.0  0.0
 0.0       0.0  0.584141  0.0  0.0
 0.725133  0.0  0.796382  0.0  0.542729

julia> sort(sprand(5, 5, .1), dims=2)
5×5 SparseMatrixCSC{Float64, Int64} with 4 stored entries:
             0.0  0.286694
                   
                   
             0.0  0.847355
                   

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugIndicates an unexpected problem or unintended behaviorperformanceMust go fastersortingPut things in order

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions