sortcols
sortcols(A, [alg=
Sort the columns of matrix A
lexicographically.
Examples
-
Sort columns lexicographically:
julia> A = [4 3 2; 1 2 3; 3 1 4]; julia> sortcols(A) 3×3 Array{Int64,2}: 1 2 3 3 1 4 4 3 2
This example sorts the columns of matrix
A
in lexicographic order. -
Sort columns with a custom comparison function:
julia> A = [4 3 2; 1 2 3; 3 1 4]; julia> sortcols(A, lt=by(x -> x % 3)) 3×3 Array{Int64,2}: 4 3 2 1 2 3 3 1 4
The
lt
argument specifies a custom comparison function. In this example, the columns are sorted based on the remainder of each element divided by 3. - Sort columns in reverse order:
julia> A = [4 3 2; 1 2 3; 3 1 4]; julia> sortcols(A, rev=true) 3×3 Array{Int64,2}: 2 3 4 3 2 1 4 1 3
By setting
rev
totrue
, the columns are sorted in reverse order.
Common mistake example:
julia> A = [4 3 2; 1 2 3; 3 1 4];
julia> sortcols(A, by=abs)
ERROR: MethodError: no method matching abs(::Array{Int64,2})
In this example, the by
argument is used incorrectly. abs
is a function that operates on individual elements, not on the entire array. Make sure to provide a valid transformation function that can be applied to each column.
See Also
find, findfirst, findin, findlast, findmin, findn, findnext, findnz, findprev, rsearch, rsearchindex, searchsorted, searchsortedfirst, searchsortedlast, sort, sort!, sortcols, sortperm, sortperm!,User Contributed Notes
Add a Note
The format of note supported is markdown, use triple backtick to start and end a code block.