From f6185a1b0f87c3c572793bccc0262f1ddfe8136e Mon Sep 17 00:00:00 2001 From: Stephan Hilb Date: Sun, 26 Aug 2018 20:06:35 +0200 Subject: [PATCH 1/2] base/sort: add sort! for multidimensional arrays --- base/sort.jl | 45 +++++++++++++++++++++++++++++++++++++++++++++ test/arrayops.jl | 5 +++++ 2 files changed, 50 insertions(+) diff --git a/base/sort.jl b/base/sort.jl index e04829a3191c4..34d76758c0205 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 diff --git a/test/arrayops.jl b/test/arrayops.jl index 3edc8516838eb..20a0fb26e3963 100644 --- a/test/arrayops.jl +++ b/test/arrayops.jl @@ -1199,11 +1199,13 @@ end @test issorted(as[:,1]) @test issorted(as[:,2]) @test issorted(as[:,3]) + @test sort!(copy(a), dims=1) == as as = sort(a, dims=2) @test issorted(as[1,:]) @test issorted(as[2,:]) @test issorted(as[3,:]) + @test sort!(copy(a), dims=2) == as local b = rand(21,21,2) @@ -1212,15 +1214,18 @@ end @test issorted(bs[:,i,1]) @test issorted(bs[:,i,2]) end + @test sort!(copy(b), dims=1) == bs bs = sort(b, dims=2) for i in 1:21 @test issorted(bs[i,:,1]) @test issorted(bs[i,:,2]) end + @test sort!(copy(b), dims=2) == bs bs = sort(b, dims=3) @test all(bs[:,:,1] .<= bs[:,:,2]) + @test sort!(copy(b), dims=3) == bs end @testset "higher dimensional sortslices" begin From 92ab0c5bce1a254b923d03f88c74b7f9dcae2576 Mon Sep 17 00:00:00 2001 From: Stephan Hilb Date: Sun, 2 Sep 2018 10:59:38 +0200 Subject: [PATCH 2/2] base/sort: mention sortslices in doctext --- base/sort.jl | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/base/sort.jl b/base/sort.jl index 34d76758c0205..dd55a0835a56f 100644 --- a/base/sort.jl +++ b/base/sort.jl @@ -938,6 +938,8 @@ Sort a multidimensional array `A` along the given dimension. See [`sort!`](@ref) for a description of possible keyword arguments. +To sort slices of an array, refer to [`sortslices`](@ref). + # Examples ```jldoctest julia> A = [4 3; 1 2] @@ -993,6 +995,8 @@ end Sort the multidimensional array `A` along dimension `dims`. See [`sort!`](@ref) for a description of possible keyword arguments. +To sort slices of an array, refer to [`sortslices`](@ref). + # Examples ```jldoctest julia> A = [4 3; 1 2]