How to sort an array of structure

How to sort an array of structure oo alphabetically by position name.

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

I tried to use the sort () function, but that didn't work?
Thanks.

+5
source share
1 answer

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

 %get the right order using second output of sort [~,index]=sort({oo.Item}) %sort it oo=oo(index) 
+6
source

Source: https://habr.com/ru/post/1215366/


All Articles