MATLAB: Sorting Variables by Value

sortvariables

I have a series of solved values I'd like to have sorted. For example,
Apple = (Apple_weight in pounds) / 2.2
Bannana = (Bannana_weight in pounds) / 2.2
Orange = (Orange_weight in pounds) / 2.2
I'd like to have these sorted by mass. In this case it doesn't matter what the mass of the apple is, just whether it is more or less than an orange. How do I get back Orange, Apple, Bannana?
*Side note: I really wish Matlab handled units*

Best Answer

You read about sort. Great. READ ALL ABOUT SORT. And get used to working with arrays, with vectors, rather than named variables, with each data point in a separate variable. MATLAB is a language that uses matrices. Things work better in MATLAB once you start to use vectors and matrices, because now you can work on an entire set of data in one operation.
fruitnames = {'Apple', 'Banana', 'Orange'};
% Just some random numbers, since I don't have a scale handy, nor an orange
% So these are just wild guesses at the weight in pounds of some phantasmic fruits
fruitweight = [.5 .4 .6];
fruitmass = fruitweight/2.205; % 2.205 pounds is roughly one kilogram mass on earth
[~,sortind] = sort(fruitmass,'descend');
fruitnames(sortind)
ans =
1×3 cell array
{'Orange'} {'Apple'} {'Banana'}
And since it is now almost time for breakfast here, all this talk about fruit is making me hungry. :) Thankfully, we have both apples and bananas available at home today, so I'll even have a choice.
Related Question