views:

57

answers:

1

My setup:

  • I have a sqlite database from which I populate a NSMutableArray of NSDictionary objects this is the DataSource for my NSTableView.
  • One of the columns holds "time", the time is a float that holds seconds.
  • I would like to display the values in this column as minutes:seconds. For instance my data would be 123.4329387 I want to display 2:03 which I have no problem doing with a subclass of NSFormatter (or NSNumberFormatter) applied to my NSTextField in the column.
  • I have sorting set up by using the Table Column Attributes in IB, I just have the sort key set to "time" and the selector set to "compare:" which works fine without the formatter.

Currently this gives me something like this when I sort (descending)

1:37, 1:31, 0:10, 0:10, 0:09, 1:30, 1:30, 1:26, 0:09

and similar nonsense, it looks like something is going on but it's definitely not sorted.

How do I get the sort to look at the underlying data instead of the formatted value? Alternately, how do I specify a custom sort method and where do I put the code for said method? I have searched around quite a bit and have not found anything to help me out with this problem, any help with this is most appreciated.

+1  A: 

It turns out that when it was sorting it was using compare: from the NSString class but it was using the underlying numeric data. I'm not exactly sure what all is going on under the hood here but I managed to fix this by creating my own compare method in a category of NSString called "timeCompare". Then in interface builder I just put "timeCompare:" in the selector field for the the table column. This gets it to call my custom compare method from which I just converted the strings given to NSNumbers and call compare: on them.

Andrew