diff --git a/base/sort.jl b/base/sort.jl index e04829a3191c4a..34d76758c0205b 100644 --- a/base/sort.jl +++ b/base/sort.jl @@ -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