Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Q: Sorting an array of structures

Subject: Q: Sorting an array of structures

From: Dick Barends

Date: 14 Oct, 1998 20:44:14

Message: 1 of 3

This is a multi-part message in MIME format.
--------------0EF27F43BBD337AEB3A311F1
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

Hello,

Is there a m-file for sorting an array of structures?

PP(i).Name
PP(i).Adress
PP(i).city
...

I want to sort on Name!

kind regards
Dick Barends


--------------0EF27F43BBD337AEB3A311F1
Content-Type: text/x-vcard; charset=us-ascii;
 name="dick.barends.vcf"
Content-Transfer-Encoding: 7bit
Content-Description: Card for Dick Barends
Content-Disposition: attachment;
 filename="dick.barends.vcf"

begin:vcard
n:Barends;Dick
x-mozilla-html:FALSE
org:EPON
adr:;;Beethovenlaan 100;6865 ED Doorwerth;;;Netherland
version:2.1
email;internet:dick.barends@wxs.nl
title:ir.
tel;fax:+31 38 4 27 29 06
tel;home:+31 26 3 34 32 05
tel;work:+31 38 4 27 29 00
x-mozilla-cpt:;0
fn:Dick Barends
end:vcard


--------------0EF27F43BBD337AEB3A311F1--

Subject: Q: Sorting an array of structures

From: psecaNS@mathworks.com (Peter Secakusuma)

Date: 14 Oct, 1998 20:10:09

Message: 2 of 3

Hi Dick:

Try this:

   [temp, idord] = sortrows({PP.Name}');
   NewPP = PP(idord);

As long as, your structure is as outlined below, that should work.

Sincerely,

Peter Secakusuma


In article <3624FF0D.D54FEED1@wxs.nl>, dick.barends@wxs.nl says...
>
>This is a multi-part message in MIME format.
>--------------0EF27F43BBD337AEB3A311F1
>Content-Type: text/plain; charset=us-ascii
>Content-Transfer-Encoding: 7bit
>
>Hello,
>
>Is there a m-file for sorting an array of structures?
>
>PP(i).Name
>PP(i).Adress
>PP(i).city
>...
>
>I want to sort on Name!
>
>kind regards
>Dick Barends
>
>
>--------------0EF27F43BBD337AEB3A311F1
>Content-Type: text/x-vcard; charset=us-ascii;
> name="dick.barends.vcf"
>Content-Transfer-Encoding: 7bit
>Content-Description: Card for Dick Barends
>Content-Disposition: attachment;
> filename="dick.barends.vcf"
>
>begin:vcard
>n:Barends;Dick
>x-mozilla-html:FALSE
>org:EPON
>adr:;;Beethovenlaan 100;6865 ED Doorwerth;;;Netherland
>version:2.1
>email;internet:dick.barends@wxs.nl
>title:ir.
>tel;fax:+31 38 4 27 29 06
>tel;home:+31 26 3 34 32 05
>tel;work:+31 38 4 27 29 00
>x-mozilla-cpt:;0
>fn:Dick Barends
>end:vcard
>
>
>--------------0EF27F43BBD337AEB3A311F1--
>

Subject: Q: Sorting an array of structures

From: Nikhil

Date: 10 Oct, 2011 17:35:30

Message: 3 of 3

An easier way to do this would be:

%Create an array
a_struct = struct('b',{1 2 3},'c',{1 2 3 4 5 6 7 8});

%Use the sort command, giving either ascend or descend based on your requirement
%Note: we require only the 'order' variable
[temp, order] = sort([a_struct(:).value],'ascend');

%Order now is an array which has the indexes of the sorted array
a_sorted = a_struct(order);

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us