Is there an efficient method to sort rows of a table, based on a particular column, in a particular order, that is not either 'ascend' or 'descend'
7 views (last 30 days)
Show older comments
Hello,
I believe this is probably an incredibly easy thing to do but it doesn't seem to be possible with sortrows function, based on the documentation.
I have a database, existing as a table variable, with about 20 columns of various data classes. Is there a way to sort the table using a specific order of a column? For example, if I have a table specified as the following:
opts = ["optionA";"optionB";"optionC"];
vals = [1 10 100];
table(opts,vals)
Is there a way to sort specifically by an externally defined order, perhaps by a new variable, such as:
order = ["optionB","optionA","optionC"]
I know that I could do this with forloops or something of that nature, but I find it hard to believe this capability doesnt already exist, and I think I am just skipping over something in the documentation. In addition to answering the question, can you please also provide the link to the documentation that shows me where I missed this in the first place?
Thanks!
0 Comments
Answers (1)
Image Analyst
on 3 Feb 2019
If you have some vector that gives the order you want the vector arranged in, just use that as an index. Example:
sortedVector = vec(sortOrder);
where sortOrder is the order you want them sorted in and vec is your original vector.
See Also
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!