如何对结构数组进行排序

Jas*_*apa 5 arrays matlab structure cell-array

如何按项目名称对oo结构数组按字母顺序排序.

oo = struct('Item', {'Quill','Ink Pen', 'Pencil'}, 'Cost', {10, 2, 1})
Run Code Online (Sandbox Code Playgroud)

我尝试使用sort()函数,但它不起作用?
谢谢.

Dan*_*iel 6

首先索引您的字段,在这种情况下oo.Items返回逗号分隔列表.对于字符串数据用于{}连接到字符串的单元格,否则用于[]获取数组:

%get the right order using second output of sort
[~,index]=sort({oo.Item})
%sort it
oo=oo(index)
Run Code Online (Sandbox Code Playgroud)