Why aren't my objects sorting with sortedArrayUsingDescriptors?

Posted by clozach on Stack Overflow See other posts from Stack Overflow or by clozach
Published on 2010-05-05T08:06:11Z Indexed on 2010/05/05 8:08 UTC
Read the original article Hit count: 314

Filed under:
|
|
|

I expected the code below to return the objects in imageSet as a sorted array. Instead, there's no difference in the ordering before and after.

NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"imageID" ascending:YES];
NSSet *imageSet = collection.images;

for (CBImage *image in imageSet) {
    NSLog(@"imageID in Set: %@",image.imageID);
}

NSArray *imageArray = [[imageSet allObjects] sortedArrayUsingDescriptors:(descriptor, nil)];    
[descriptor release];

for (CBImage *image in imageArray) {
    NSLog(@"imageID in Array: %@",image.imageID);
}

Fwiw, CBImage is defined in my core data model. I don't know why sorting on managed objects would work any differently than on "regular" objects, but maybe it matters.

As proof that @"imageID" should work as the key for the descriptor, here's what the two log loops above output for one of the sets I'm iterating through:

2010-05-05 00:49:52.876 Cover Browser[38678:207] imageID in Array: 360339
2010-05-05 00:49:52.876 Cover Browser[38678:207] imageID in Array: 360337
2010-05-05 00:49:52.877 Cover Browser[38678:207] imageID in Array: 360338
2010-05-05 00:49:52.878 Cover Browser[38678:207] imageID in Array: 360336
2010-05-05 00:49:52.879 Cover Browser[38678:207] imageID in Array: 360335

... For extra credit, I'd love to get a general solution to troubleshooting NSSortDescriptor troubles (esp. if it also applies to troubleshooting NSPredicate). The functionality of these things seems totally opaque to me and consequently debugging takes forever.

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about iphone