%% Sort Rows of Table
% Sort rows of heterogeneous data in a table.
% Create a table from the patients.mat data set, which includes basic health information for a group of patients. Include the patients age, gender, height, and their self-assessed health status in the table. Make the SelfAssessedHealthStatus variable an ordinal categorical array.

load patients
vals = {'Poor','Fair','Good','Excellent'};
SelfAssessedHealthStatus = categorical(SelfAssessedHealthStatus,vals,'Ordinal',true);
T = table(Age,Gender,Height,SelfAssessedHealthStatus);

%
% Find the top 10 rows when the table is sorted in descending order. The result is sorted by the first variable, Age, in descending order. The remaining columns are subsorted to break ties:
% The Gender variable is subsorted to break ties with age.
% The Height variable breaks ties with gender.
% The SelfAssessedHealthStatus variable breaks ties with height.

TA = topkrows(T,10)

%
% Find the top 10 rows containing the youngest women by sorting on the Gender variable and subsorting on the Age variable.

TB = topkrows(T,10,{'Gender','Age'},'ascend')

%
% Find the top 10 oldest women by changing the sorting direction of the Age variable to 'descend'.

TB = topkrows(T,10,{'Gender','Age'},{'ascend','descend'})

% Copyright 2012 The MathWorks, Inc.