MATLAB: How to sort rows by the first digit of each number

matrixmatrix manipulationorder of magnitudesortsorting

I have a .csv file called "SORT.csv" (attached). I imported SORT.csv to MATLAB as a 128×5 matrix (the 5 columns being ID, T, N, P, D).
The "ID" columns contains numbers like 762035, 325, 45, 221, 53 etc.
I would like to sort the matrix based on the first digit of the numbers in the "ID" column. In other words, I would like my SORT matrix to have rows in the following order (based on the examples given above):
  1. 221
  2. 325
  3. 45
  4. 53
  5. 762035
Would someone be able to help me accomplish that?

Best Answer

Simplest, but probably not the fastest:
[~, order] = sort(compose('%d', yourarray(:, 1)));
yourarray = yourarray(order, :);
Probably faster:
[~, order] = sort(floor(yourarray(:, 1) ./ 10 .^ ceil(log10(yourarray(:, 1)) - 1)));
yourarray = yourarray(order, :);