• 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: "Adam R. Maxwell" <email@hidden>
  • Date: Sat, 8 Dec 2007 21:34:19 -0800


On Dec 8, 2007, at 8:36 PM, Bill Garrison wrote:

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 >

There are some potentially serious performance problems if you subclass NSSortDescriptor. If your sort key is a keypath and you have a large array, you can blow out your autorelease pool on 10.4 because Foundation's valueForKeyPath: implementation is inefficient (this is fixed in 10.5). Plus, you don't get the benefit of NSArray's sort cache so it's abysmally slow.


I was unable to get [super compareObject:obj1 toObject:obj2] to work if the key was a keypath, so had to implement it like this:

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

    NSString *keyPath = [self key];
    id value1, value2;

    value1 = [object1 valueForKeyPath:keyPath];
    value2 = [object2 valueForKeyPath:keyPath];

// header says keys may be key paths, but super doesn't work correctly when I pass in a key path; therefore, we'll just ignore super altogether
typedef NSComparisonResult (*comparatorIMP)(id, SEL, id);


SEL selector = [self selector];
comparatorIMP comparator = (comparatorIMP)[value1 methodForSelector:selector];
NSComparisonResult result = comparator(value1, selector, value2);


    return [self ascending] ? result : (result *= -1);
}

I put together a demo project for this at http://homepage.mac.com/amaxwell/.Public/kvcTest.zip but haven't looked at it for a long time. YMMV.

--
adam


_______________________________________________

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: Bill Garrison <email@hidden>
References: 
 >NSTableView, NSArrayController and secondary sortings (From: Adhamh Findlay <email@hidden>)
 >Re: NSTableView, NSArrayController and secondary sortings (From: Bill Garrison <email@hidden>)

  • Prev by Date: Re: detecting no internet connection
  • Next by Date: Re: NSTableView, NSArrayController and secondary sortings
  • Previous by thread: Re: NSTableView, NSArrayController and secondary sortings
  • Next by thread: Re: NSTableView, NSArrayController and secondary sortings
  • Index(es):
    • Date
    • Thread