Search code examples
objective-ccocoasortingnstableviewnsarraycontroller

Auto-sort table column contents bound to NSArrayController


So, this my case :

I've got an NSMutableArray (of NSMutableDictionary instances) bound to an NSArrayController. Each element's name value is, in turn, bound to the value of the first table column.

The thing is :

How should I make it auto-sort the elements in that specific table column (of my NSTableView) ?


Solution

  • Is the column sortable through user action? For the table column's Value binding, you can enable the Creates Sort Descriptor option. That's enabled by default, usually. You should also set the Sort Key and Selector in the attributes inspector.

    If you're trying to make the table sorted when the window is first loaded, you should construct an array of NSSortDescriptors and pass that to the array controller's -setSortDescriptors: method. A good time to do this is in an override of -windowDidLoad in the window controller or during -awakeFromNib.