Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort a structure array

How do I sort the oo structure array alphabetical order by item name.

oo = struct('Item', {'Quill','Ink Pen', 'Pencil'}, 'Cost', {10, 2, 1})

I tried using the sort() function but it didn't work?
Thank you.

like image 945
Jason Thapa Avatar asked Mar 14 '15 23:03

Jason Thapa


1 Answers

First index your field, in this case oo.Items which returns a comma separated list. For string data use {} to concatenate to a cell of strings, otherwise use [] to get an array:

%get the right order using second output of sort
[~,index]=sort({oo.Item})
%sort it
oo=oo(index)
like image 155
Daniel Avatar answered Oct 08 '22 02:10

Daniel