Matlab - sort cell array of objects by property

前端 未结 1 691
北海茫月
北海茫月 2021-01-13 18:39

Suppose I had a class named Foo, with a datenum property named DateTime. If I had a cell array collection of Foo objects, how would I sort that according to each object\'s D

相关标签:
1条回答
  • 2021-01-13 19:12

    The simplest approach is to extract the time-values into a vector, sort that, and use the new order to sort the original array.

    %# extract DateTime from the cell array fooCell
    dateTime = cellfun(@(x)x.DateTime, fooCell);
    
    [~,sortIdx] = sort(dateTime);
    
    %# reorder fooCell
    fooCell = fooCell(sortIdx);
    
    0 讨论(0)
提交回复
热议问题