MATLAB: How to sort a structure array based on a specific field

MATLAB

How do we sort a structure array based on a specific field?

Best Answer

We have an instruction blog on how to do that in older versions of MATLAB (before R2013b):
If you are using a MATLAB version newer than R2013b, you can take advantage of the "sortrows" function in tables for a simpler workflow:
>> % suppose 's' is the struct array. 'DOB' is the field that contains date and time.
>> T = struct2table(s); % convert the struct array to a table
>> sortedT = sortrows(T, 'DOB'); % sort the table by 'DOB'
>> sortedS = table2struct(sortedT) % change it back to struct array if necessary