How to sort a structure array

情到浓时终转凉″ 提交于 2019-12-07 06:02:47

问题


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.


回答1:


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)


来源:https://stackoverflow.com/questions/29055332/how-to-sort-a-structure-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!