• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSTableView, NSArrayController and secondary sortings
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSTableView, NSArrayController and secondary sortings


  • Subject: Re: NSTableView, NSArrayController and secondary sortings
  • From: Bill Garrison <email@hidden>
  • Date: Sat, 8 Dec 2007 23:36:32 -0500


On Dec 8, 2007, at 11:00 AM, Adhamh Findlay wrote:

Hello,

I have an NSTableView that is bound to an NSArrayController. There are 5 columns in the table view, one of which is an NSDate. What I would like to do is always have the secondary sort be the date column. The default behavior seems to be that the secondary sort is based on the previously selected column.

I have setup sort keys on all the columns in IB and tired various things with setSortDescriptors: on NSTableView and NSArrayController, but I'm not having any luck. It seems like what I really need it sortDescriptorsDidChange: from the NSTableDataSource protocol, but since I'm using bindings that's not available.

Does anyone have suggestions on how to do programmatically set the secondary sort descriptor in this situation?


You try handling this in a subclass of NSArrayController. Override - arrangedObjects to always apply the sort-by-date to the arrangement returned by [super arrangedObjects]. Or override -sortDescriptors to add a sort-by-date descriptor at the end of the array of descriptors that [super sortDescriptors] would return. This might be undesirable, though, if you don't want the sort-by-date behavior to always apply when using this array controller.

---

Maybe what you want to do could also be achieved by subclassing NSSortDescriptor, applying a sort-by-date after the 'primary' sorting? Using an NSSortDescriptor subclass would be bindings- compatible.

<http://www.fatcatsoftware.com/blog/2007/subclassing-nssortdescriptor-for-fun-and-profit >

I think you'd have to override -compareObject:toObject: to apply the sort-by-date condition after the primary comparison.

Typed in Mail, YMMV:

- (NSComparisonResult) compareObject:(id)object1 toObject:(id)object2
{
	NSComparisonResult finalResult;

	// Apply 'primary' sort
	finalResult = [super compareObject:object1 toObject:object2];

// Apply the sort-by-date if needed; i.e. objects that would otherwise initially compare as equal are further sorted by date.
if ( NSOrderedSame == finalResult ) {
// compare objects by date, ascending or descending order as needed
finalResult = ...; //
}


	return finalResult;
}


--- Bill _______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: NSTableView, NSArrayController and secondary sortings
      • From: "Adam R. Maxwell" <email@hidden>
References: 
 >NSTableView, NSArrayController and secondary sortings (From: Adhamh Findlay <email@hidden>)

  • Prev by Date: Re: Debugger exits with signal 5 (SIGTRAP) after computer crashed.
  • Next by Date: Re: detecting no internet connection
  • Previous by thread: NSTableView, NSArrayController and secondary sortings
  • Next by thread: Re: NSTableView, NSArrayController and secondary sortings
  • Index(es):
    • Date
    • Thread