Discussion:
sorting a structure
(too old to reply)
Bruce Bowler
2017-12-06 20:44:30 UTC
Permalink
Raw Message
Is there an easier way to sort a structure based on a field other than to
sort that field on it's own, saving the second output of sort and then
using that to index the structure?

Specifically, I'd like to get two different 'dir's, slam them together and
then sort on datenum.

Thanks!
Bruce
dpb
2017-12-16 15:55:50 UTC
Permalink
Raw Message
Post by Bruce Bowler
Is there an easier way to sort a structure based on a field other than to
sort that field on it's own, saving the second output of sort and then
using that to index the structure?
Specifically, I'd like to get two different 'dir's, slam them together and
then sort on datenum.
...

Sure, that's no problem at all...since the two directory structures will
have same fields, just concatenate and sort the result...in explicit
form it would look like

d1=dir('*.m');
d2=dir('*.txt');
d=[d1;d2];
[~,ixd]=sort([d.datenum]);

then process the combined directory structure d in the sequence returned.

--
Bruce Bowler
2017-12-18 18:01:39 UTC
Permalink
Raw Message
Post by dpb
Post by Bruce Bowler
Is there an easier way to sort a structure based on a field other than
to sort that field on it's own, saving the second output of sort and
then using that to index the structure?
Specifically, I'd like to get two different 'dir's, slam them together
and then sort on datenum.
...
Sure, that's no problem at all...since the two directory structures will
have same fields, just concatenate and sort the result...in explicit
form it would look like
d1=dir('*.m');
d2=dir('*.txt');
d=[d1;d2];
[~,ixd]=sort([d.datenum]);
then process the combined directory structure d in the sequence returned.
Which is, essentially, what I suggested...

The method I got from support was to turn it into a table, sort the table,
and then turn it back into a structure...

d1 = dir('*.m');
d2 = dir('*.txt');
d = [d1;d2];
d = table2struct(sortrows(struct2table(d),'datenum'));

Bruce
dpb
2017-12-19 15:01:11 UTC
Permalink
Raw Message
...
Post by Bruce Bowler
Post by dpb
Sure, that's no problem at all...since the two directory structures will
have same fields, just concatenate and sort the result...in explicit
form it would look like
d1=dir('*.m');
d2=dir('*.txt');
d=[d1;d2];
[~,ixd]=sort([d.datenum]);
then process the combined directory structure d in the sequence returned.
Which is, essentially, what I suggested...
The method I got from support was to turn it into a table, sort the table,
and then turn it back into a structure...
d1 = dir('*.m');
d2 = dir('*.txt');
d = [d1;d2];
d = table2struct(sortrows(struct2table(d),'datenum'));
Which is more overhead than the "dead ahead" solution I'd venture by a
measurable mount owing to the cast to/from the struct. You still have
SORT and then to iterate over the struct anyway, so what's the
difference in using the index vis a vis the loop variable?

If I were doing this I'd just cut out the intermediary step and write

d=[dir('*.m');dir('*.txt')];
[~,ixd]=sort([d.datenum]);

--

Loading...