MATLAB: How to sortrows by specific strings

MATLABsortsorting stringssortrowstable

I have a table filled with strings and would like to sort the rows by specific strings. Is there a way to use sortrows or sort in order to do that? I am open to all and better ways of doing this. For example:
% Create Data
Position = {'Vice President';'Guest';'Member';'President'};
Name = {'John';'Jane';'Nancy';'Bob'};
% Turn data into a table
data = table(Position,Name);
% Sort rows by President->VP->Member->Guest
%%%% Your Helpful Advice would go here :) %%%%
Desired Output
%{
data =
4×2 table
Position Name
__________________ _________
{'President' } {'John' }
{'Vice President'} {'Jane' }
{'Member' } {'Nancy'}
{'Guest' } {'Bob' }
%}
I originally created a cell array of each position, putting them in the order that I desired, and using sortrows, but couldn't figure out the syntax so, I figured it wasn't possible considering the direction must be 'ascend' or 'descend'.
I could possibly just assign a value to each string using strcmp and a swtich, add that column to the data, then sort the rows by the new column.
Any suggestions are appreciated, thank you!

Best Answer

desired_order = {'President','Vice President','Member','Guest'};
Position = {'Vice President';'Guest';'Member';'President'};
Name = {'John';'Jane';'Nancy';'Bob'};
Turn the Position variable into a categorical array whose order matters (the array is an ordinal array.) The ordering is given by the desired_order list.
ranks = categorical(Position, desired_order, 'ordinal', true)
ranks = 4×1 categorical array
Vice President Guest Member President
Store the data in a table array.
results = table(ranks, Name, 'VariableNames', ["Rank", "Name"])
results = 4x2 table
Rank Name ______________ _________ Vice President {'John' } Guest {'Jane' } Member {'Nancy'} President {'Bob' }
Sort the table by Rank (taking advantage of the fact that it is ordinal.)
results = sortrows(results, "Rank")
results = 4x2 table
Rank Name ______________ _________ President {'Bob' } Vice President {'John' } Member {'Nancy'} Guest {'Jane' }
Who's the Member?
results{results.Rank == 'Member', 'Name'}
ans = 1x1 cell array
{'Nancy'}