Skip navigation.
 
mlRe: sort descriptor optimization
FROM : James Bucanek
DATE : Wed Jul 05 16:23:13 2006

Adam R. Maxwell wrote on Tuesday, July 4, 2006:

>We considered that.  However, our users want nil values and empty 
>strings to sort last in an a-z ordered list, instead of first, for 
>all objects.  Implementing this in NSSortDescriptor was also less 
>code, and doesn't require adding a special category on every object 
>that we sort.
>

>> As for some of your comparands being nil values, the obvious 
>> suggestion is,
>> Don't Do That. m.

>
>Thanks, but that is not an option.  We need valueForKey: to return 
>nil when there is no value for that key.


Apologies for jumping into this thread late, but I usually solve these kinds of problems by creating another method on the object just for sorting, such as

    - (id)sortableaValueForKey:(NSString*)key
    {
        id value = [self valueForKey:key];
        if (value==nil)
            value = [NSNull or whatever];
        return (value);
    }

You can continue to use valueForKey: programmatically the way you do now, but when you sort specify sortableValueForKey: as the property to sort on.

I have several data models for tables that return NSImages for some cells (indicating the status of the record in the table) via the method -(NSImage*)statusImage. Obviously, this is not suitable for sorting, so I create another property (-(int)sortableStatus) that returns a numeric value for each possible status condtion.

Create this as a category if you want to keep your model/view/sorting code separate from the implementation of your object.

--
James Bucanek

Related mailsAuthorDate
mlsort descriptor optimization Adam R. Maxwell Jul 3, 01:53
mlRe: sort descriptor optimization Adam R. Maxwell Jul 4, 19:44
mlRe: sort descriptor optimization Matt Neuburg Jul 5, 00:00
mlRe: sort descriptor optimization Adam R. Maxwell Jul 5, 00:43
mlRe: sort descriptor optimization Matt Neuburg Jul 5, 01:44
mlRe: sort descriptor optimization Adam R. Maxwell Jul 5, 02:16
mlRe: sort descriptor optimization James Bucanek Jul 5, 16:23