File Exchange

image thumbnail

Nested sort of structure arrays

version 1.3 (6.48 KB) by

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



View License

Editor's Note: This file was selected as MATLAB Central Pick of the Week

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.

Comments and Ratings (5)

Viveka Mayya

great utility. thanks.

Jake Hughey

Jake Hughey (view profile)

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.

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.

Jake Hughey

Jake Hughey (view profile)

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?

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!



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


Fixed nestedSortStruct so column struct arrays are sorted properly.


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

MATLAB Release
MATLAB 7.7 (R2008b)

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

» Watch video

Win prizes and improve your MATLAB skills

Play today