Can't get sortperm to return the sorting order of columns of a matrix

Something I didn’t understand at first—a fundamental misunderstanding—sort(t,dims=2) does not preserve the columns necessarily. It sorts the columns of each row separately. What I really wanted was to sort the entire columns (preserving their contents).

julia> t
3×2 Matrix{Float64}:
 0.5  0.5
 0.5  0.0
 0.0  0.5

julia> sort(t,dims=2) # Doesn't preserve the columns
3×2 Matrix{Float64}:
 0.5  0.5
 0.0  0.5
 0.0  0.5

julia> sort(collect(eachslice(t,dims=2))) # Does preserve the columns
2-element Vector{SubArray{Float64, 1, Matrix{Float64}, Tuple{Base.Slice{Base.OneTo{Int64}}, Int64}, true}}:
 [0.5, 0.0, 0.5]
 [0.5, 0.5, 0.0]
1 Like