Sort columns of a table by the value corresponding
Show older comments
Ive attached a sc of my table, although it continues to 86 columns, I would like to sort the values corresponding to each variable in decending order, and have the variable names be sorted along with the value.
The table dimensions is 1x86 as seen in the screenshot

3 Comments
the cyclist
on 20 Feb 2023
I don't really follow what you want. Do you want each column sorted independently from the others?
And then you want the columns sorted alphabetically according to the column names (e.g. Ac first, Zr last)?
Out of curiosity ... why? Guessing homework.
Turner
on 20 Feb 2023
If the table was arranged transposed, then this would be trivial using SORTROWS():
V = [0.141;0;0.0146;0.0045;0.0567];
T = table(V,'RowNames',{'H','He','Li','Be','B'})
T = sortrows(T,'V')
Better data design makes code simpler, more robust, and more efficient.
Accepted Answer
More Answers (1)
% I make a table of random data with 10 columns and 1 row, to represent your table
C = num2cell(rand(1,10));
T = table(C{:},'VariableNames',cellstr(('A':'J').'))
% sort the values, descending
[~,idx] = sort(T{:,:},'descend');
% reorder the table columns
T = T(:,idx)
4 Comments
Turner
on 20 Feb 2023
Ron
on 28 Jun 2024
I have this data. the last row (the one highlighted in blue) is the sum of all the values in each column. I want to re-arrange all the columns as per the descending values in this last row but I am unable to do so. I tried your code and it did work for all other tables but I dont know why its not working for my data. Can you please help?

Walter Roberson
on 28 Jun 2024
sortrows(Data, 9, 'descending')
Ron
on 29 Jun 2024
Thankyou so much for taking out time and replying to my question. Although I was able to find out that one of the elements was a string instead of numeric and thats why I was not able to sort to but thankyou once again for helping.
Categories
Find more on Shifting and Sorting Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!