如何排序的结构数组 [英] How to sort a structure array

查看:144
本文介绍了如何排序的结构数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何排序项目名称面向对象的结构数组字母顺序排列。

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

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

我尝试使用sort()函数,但没有奏效?结果
谢谢你。

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

推荐答案

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

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)

这篇关于如何排序的结构数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆