MATLAB: Multi-Column Ordering of Matrices in Different Orders

MATLABmulti-column orderingmultiple order directions

Hi,
I would like to re-order a two-dimensional array in ascending order with respect to the first column, and in descending order with respect to the second column.
For instance, if my initial matrix is equal to A = [1 , 1; 2 , 1; 1 , 2; 2 , 2; 1 , 3; 2 , 3], I want the final matrix to be equal to A = [2 , 1; 2 , 2; 2 , 3; 1 , 1; 1 , 2; 1 , 3].
I would also need to keep track of the permutation of the row indices.

Best Answer

[newA, ordering] = sortrows(A, [1 -2])
to get "in ascending order with respect to the first column, and in descending order with respect to the second column" and
[newA, ordering] = sortrows(A, [-1 2])
to get the actual result you asked for (which is the opposite of what you said)