View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
Nested sort of structure arrays

5.0 | 1 rating Rate this file 53 Downloads (last 30 days) File Size: 6.48 KB File ID: #28573 Version: 1.3

Nested sort of structure arrays



27 Aug 2010 (Updated )

Perform a nested sort of a struct array based on multiple fields.

Editor's Notes:

This file was selected as MATLAB Central Pick of the Week

| Watch this File

File Information

nestedSortStruct and nestedSortStruct2 do a nested sort of a one-dimensional struct array by two different methods. That is, the struct array is sorted by one field, then those entries of the array that have the same value for that field are sorted by a second field, etc.

If you want to sort by only one field, you can call nestedSortStruct and use a simple string instead of a cell array for fieldNamesCell, or you can simply call sortStruct.

The restrictions are that the fields must be single numbers or logicals, or chars (usually simple strings).

By default the struct array will be sorted in ascending order, but the functions include an option to sort in descending order.

nestedSortStruct will usually be faster than nestedSortStruct2. For nestedSortStruct, the speed of sorting is mostly independent of the order of the fieldnames in fieldNamesCell.

For nestedSortStruct2, the order of the fields in fieldNamesCell affects the speed. The sooner a field for which most entries in the struct array have unique values will be used to sort the struct array (i.e., the earlier its location in fieldNamesCell), the faster nestedSortStruct2 will be. If a field with mostly unique entries is the first field by which the struct array will be sorted, nestedSortStruct2 could be faster than nestedSortStruct.

nestedSortStruct could call sortStruct, and nestedSortStruct2 does call sortStruct2.

MATLAB release MATLAB 7.7 (R2008b)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (5)
09 Jan 2017 Viveka Mayya

great utility. thanks.

22 Mar 2016 Jake Hughey

Ah, the problem is you're not creating a structure array, you're creating a single structure in which each field is an array.

example1 = struct('name', {'a', 'b', 'c'}, 'time', {1, 2, 3})

example2 = struct = {'a', 'b', 'c'}
example2.time = [1, 2, 3]

Notice the difference between these two? nestedSortStruct is designed to handle the first, not the second.

Comment only
22 Mar 2016 Jeff Lapierre

Thanks for the quick reply.

1.Example Code:
example.year(1:10) = 2015 = linspace(1,10,10)
example.lon(1:10) = linspace(1,10,10)
example.curr(1:10) = linspace(1,10,10)
example.time(1:10) = linspace(1,10,10)
datasort = nestedSortStruct(example,{'time'})

The above code gives the same error for me. i.e., "time is not a valid fieldname by which to sort."

2. nestedSortStruct2 gives the same error, with or without cell brackets.

3. Same as 2.

4. R2015b.

Comment only
22 Mar 2016 Jake Hughey

Jeff - I no longer have access to Matlab, but I can try to help.

1. Can you give me a minimal working example that reproduces the error?
2. Have you tried nestedSortStruct2?
3. Have you tried sortStruct on the individual fields?
4. What version of Matlab are you using?

Comment only
21 Mar 2016 Jeff Lapierre

I am trying to sort a structure with many fields (year,day,month,hour,minute,second,lat,lon,etc) in chronological order. The command I am using is:

datasort = nestedSortStruct(data,{'year','mon','day','hour','min','sec'})

, but I am getting errors saying that all the fieldnames are not valid by which to sort. I have checked, and all the fields are there, and are all doubles. Let me know if you can help me out. Thanks in advance!

Comment only
31 Aug 2010 1.1

Minor fix to formatting of initial comments in nestedSortStruct2.m.

16 Sep 2010 1.2

Fixed nestedSortStruct so column struct arrays are sorted properly.

17 Sep 2010 1.3

Simplified nestedSortStruct to avoid converting from cell to matrix, resulting in ~20% speedup. Thanks Jori. Fixed minor naming inconsistencies in nestedSortStruct2.

Contact us