如何对结构数组进行排序

时间:2022-04-28 18:01:32

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

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

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

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

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

1 个解决方案

#1


6  

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:

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

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

#1


6  

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:

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

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