sortrows

sortrows(A, [alg=,] [by=,] [lt=,] [rev=false])

Sort the rows of matrix A lexicographically.

Examples

  1. Sort rows lexicographically:

    julia> A = [3 2 1; 6 5 4; 9 8 7];
    julia> sortrows(A)
    3×3 Array{Int64,2}:
    3  2  1
    6  5  4
    9  8  7

    This example sorts the rows of matrix A in lexicographic order.

  2. Sort rows based on specific columns:

    julia> B = [3 2 1; 6 5 4; 9 8 7];
    julia> sortrows(B, by = x -> x[2])
    3×3 Array{Int64,2}:
    3  2  1
    9  8  7
    6  5  4

    By using the by argument, you can specify a transformation x -> x[2] to sort the rows based on the second column.

  3. Sort rows in reverse order:

    julia> C = [9 8 7; 6 5 4; 3 2 1];
    julia> sortrows(C, rev = true)
    3×3 Array{Int64,2}:
    9  8  7
    6  5  4
    3  2  1

    Setting rev = true sorts the rows in reverse order.

  4. Specify sorting algorithm:
    julia> D = [9 8 7; 6 5 4; 3 2 1];
    julia> sortrows(D, alg = QuickSort)
    3×3 Array{Int64,2}:
    3  2  1
    6  5  4
    9  8  7

    The alg argument allows you to specify the sorting algorithm. In this example, QuickSort is used.

Common mistake example:

julia> E = [3 2 1; 6 5 4; 9 8 7];
julia> sortrows(E, by = x -> x[4])
ERROR: BoundsError: attempt to access 3×3 Array{Int64,2} at index [CartesianIndex(1, 4)]

In this example, the by argument is attempting to access the fourth element of each row, but the matrix E only has three columns. It's important to ensure that the specified column index is within the valid range of the matrix before using sortrows.

See Also

Ac_ldiv_B, Ac_ldiv_Bc, Ac_mul_B, Ac_mul_Bc, Ac_rdiv_B, Ac_rdiv_Bc, At_ldiv_B, At_ldiv_Bt, At_mul_B, At_mul_Bt, At_rdiv_B, At_rdiv_Bt, A_ldiv_Bc, A_ldiv_Bt, A_mul_B!, A_mul_Bc, A_mul_Bt, A_rdiv_Bc, A_rdiv_Bt, Bidiagonal, cond, conv2, det, diag, diagind, diagm, diff, eig, eigvals, eigvecs, expm, eye, full, inv, isdiag, ishermitian, isposdef, isposdef!, issym, istril, istriu, logabsdet, logdet, lyap, norm, qrfact, rank, repmat, rot180, rotl90, rotr90, sortrows, sqrtm, SymTridiagonal, trace, Tridiagonal, tril, tril!, triu, triu!, writedlm,

User Contributed Notes

Add a Note

The format of note supported is markdown, use triple backtick to start and end a code block.

*Required Field
Details

Checking you are not a robot: