Skip to content

Commit

Permalink
base/sort: add sort! for multidimensional arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
stev47 committed Aug 26, 2018
1 parent a2a1506 commit ff724bb
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions base/sort.jl
Original file line number Diff line number Diff line change
Expand Up @@ -987,6 +987,51 @@ end
Av
end

"""
sort!(A; dims::Integer, alg::Algorithm=defalg(v), lt=isless, by=identity, rev::Bool=false, order::Ordering=Forward)
Sort the multidimensional array `A` along dimension `dims`.
See [`sort!`](@ref) for a description of possible keyword arguments.
# Examples
```jldoctest
julia> A = [4 3; 1 2]
2×2 Array{Int64,2}:
4 3
1 2
julia> sort!(A, dims = 1); A
2×2 Array{Int64,2}:
1 2
4 3
julia> sort!(A, dims = 2); A
2×2 Array{Int64,2}:
1 2
3 4
```
"""
function sort!(A::AbstractArray;
dims::Integer,
alg::Algorithm=defalg(A),
lt=isless,
by=identity,
rev::Union{Bool,Nothing}=nothing,
order::Ordering=Forward)
ordr = ord(lt, by, rev, order)
nd = ndims(A)
k = dims

1 <= k <= nd || throw(ArgumentError("dimension out of range"))

remdims = ntuple(i -> i == k ? 1 : size(A, i), nd)
for idx in CartesianIndices(remdims)
Av = view(A, ntuple(i -> i == k ? Colon() : idx[i], nd)...)
sort!(Av, alg, ordr)
end
A
end

## fast clever sorting for floats ##

module Float
Expand Down

0 comments on commit ff724bb

Please sign in to comment.