Sorting tables bound to Core Data


Hello, I think I have a reasonable grasp of the basics here, but I’m trying to push beyond that. Got a few related questions regarding sorting the Core Data-based listing in the CarLot program:

How do I present the listings in the table pre-sorted on load?

How do I allow sorting by clicking on the column header?

What if I want to sort by multiple columns?

I’m probably staring the answer in the face somewhere here, but thanks in advance for any advice/pointers…


For sorting on table column clicks (and multiple columns), check out the discussion on using NSSortDescriptors with NSTableView on p.142 of Chapter 8. For some reason this behavior is done for you with cell-based table views, but not with view-based table views – hopefully it this will be improved upon in future versions of Mac OS X.

You can initialize the sort order using NSTableView’s -setSortDescriptors:

NSSortDescriptor *sd = [NSSortDescriptor sortDescriptorWithKey:@"keyName" ascending:YES]; [tableView setSortDescriptors:[NSArray arrayWithObject:sd]];

If you want to sort on multiple columns, you can use multiple sort descriptors together.



I’m a little confused about this same issue.

Specifically, given the CarLot application created exactly as instructed in the chapter, how would you go about giving it sorting capabilities? I’ve tried using the Interface Builder with Sort Keys and Selectors, but all I get is the ability to click on the column header and change the sorting arrow direction; the rows don’t move at all … so I must be doing something wrong.

Any help would be greatly appreciated. Thanks!


View based table views don’t perform sorting “out of the box”; you need to help them along a bit. In order to add sorting to the CarLot example, you will need to do a few things:

  1. Make MyDocument the dataSource of the NSTableView.
  2. Add an outlet to the Cars array controller.
  3. Implement tableView:sortDescriptorsDidChange:

- (void)tableView:(NSTableView *)tableView sortDescriptorsDidChange:(NSArray *)oldDescriptors { [carsArrayController setSortDescriptors:[tableView sortDescriptors]]; }

  1. Set Sort Keys on the table columns.


Thanks, Adam, for that response explaining that the tableView:sortDescriptorsDidChange: method needed to be implemented for sorting to actually work. Works great now!



Erik, could you please upload your CarLot w/sorting project file so I could look at it? Something, likely something very simple, just is not clicking for me. Thanks in advance…


You can grab it here:



Before checking this thread and seeing your suggested solution I achieved (or at least seem to have achieved) sorting more simply as follows:

In the Bindings Inspector for the Table View I bound the Sort Descriptors to the Cars array controller with Controller Key sortDescriptors.

Then in the Attributes Inspector for the table columns I entered the following:
Column 0, Sort Key makeModel, Selector caseInsensitiveCompare:
Column 1, Sort Key price, Selector compare:
Column 2, Sort Key onSpecial, Selector compare:

Is there a reason the above approach is incorrect or problematic relative your suggested solution?


IMHO JefferyRPrice presents the elegant solution to sort columns in a view based NSTableView using CoreData bindings in IB without code. My experience with another app was that this only started to work after all columns had their key set.

The proposed solution by Adam seems to refer to an older way to do these things, he might be using a cell-based table view.


This just saved my day!