I have table with 16 columns, first three are input columns. I want to sort whole table according to first three columns at a time.
T = table(a1, a2, a3, ..., a16)
All a1
, a2
, and a3
will be sorted in ascending order.
For example:
a1 = [6 3 9 6 3 9 6 5]'
a2 = [7 8 2 3 7 7 6 7]'
a3 = [9 2 3 3 4 3 7 4]'
The output shall be:
a1 = [3 3 5 6 6 6 9 9]'
a2 = [7 8 7 3 6 7 2 7]'
a3 = [4 2 4 3 7 9 3 3]'
I think the function you are looking for is sortrows. e.g.
a1 = [6 3 9 6 3 9 6 5]';
a2 = [7 8 2 3 7 7 6 7]';
a3 = [9 2 3 3 4 3 7 4]';
temp = table(a1,a2,a3);
sortrows(temp,[1,2,3])
where you supply a vector of the columns you want to sort by.
This gives you
ans =
8×3 table
a1 a2 a3
__ __ __
3 7 4
3 8 2
5 7 4
6 3 3
6 6 7
6 7 9
9 2 3
9 7 3
,sorted first by column 1, then column 2, and finally column 3.