2.30 Sort a matrix row-wise using first column as key

Given

     4     2     5 
     2     7     9 
    10     1     2
 

Sort the matrix row-wise using first column as key so that the result is

     2     7     9 
     4     2     5 
    10     1     2
 

In Matlab, the sortrows() command is used. In Mathematica the Sort[] command is used as is. No need to do anything special.

Mathematica

mat={{4, 2, 5}, 
     {2, 7, 9}, 
     {10,1, 2}}; 
Sort[mat]
 

{{2,  7, 9}, 
{4,  2, 5}, 
{10, 1, 2}}
 

 

Matlab

A=[4  2 5; 
   2  7 9; 
   10 1 2]; 
sortrows(A)
 

     2     7     9 
     4     2     5 
    10     1     2
 

 

Maple

restart; 
A:=Matrix([[4,2,5],[2,7,9],[10,1,2]]); 
convert(A,listlist); 
sort(%); 
Matrix(%)
 

Matrix(3, 3, [[2, 7, 9], 
             [4, 2, 5], 
             [10, 1, 2]])
 

 

In Maple 2023 we can now do the following to get the same output

restart; 
A:=Matrix([[4,2,5],[2,7,9],[10,1,2]]); 
ArrayTools:-SortBy( A, 'column', 1 );
 

Matrix(3, 3, [[2, 7, 9], 
             [4, 2, 5], 
             [10, 1, 2]])