如何按项目名称的字母顺序对 oo 结构数组进行排序。
oo = struct('Item', {'Quill','Ink Pen', 'Pencil'}, 'Cost', {10, 2, 1})
我尝试使用 sort() 函数但它不起作用?
谢谢。
最佳答案
首先索引您的字段,在本例中为 oo.Items
,它返回一个逗号分隔的列表。对于字符串数据,使用 {}
连接到字符串单元格,否则使用 []
获取数组:
%get the right order using second output of sort
[~,index]=sort({oo.Item})
%sort it
oo=oo(index)
关于arrays - 如何对结构数组进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29055332/