NSSortDescriptor not working with array of custom classes

  • Hi

    I have an NSMutableArray containing instances of a custom "Page"
    class and can't get NSSortDescriptor to do anything. The page class
    has an accessor method for the property I want to sort.

    - (id) pageNumber
    {
    return [pageRecord objectForKey: @"page_number"];
    }

    In the object that contains the page array, the sort descriptor array
    is initialized like this

    NSArray        *sortDescriptors = [NSArray arrayWithObject:
    [[NSSortDescriptor alloc] initWithKey: @"pageNumber" ascending: YES]];

    And I try to use it like this

    NSLog(@"before sorting: %@", selectedPages);

    [selectedPages sortUsingDescriptors: sortDescriptors];

    NSLog(@"after sorting: %@", selectedPages);

    Regardless of the order in which I select pages, the before and after
    arrays are identical. Anyone see where I'm messing up?

    Thanks for any help

    Ken
  • I managed to get it working but don't know why this fails

    - (id) pageNumber
    {
    return [pagePropertiesDict objectForKey: @"page_number"]; // returns
    an NSNumber
    }

    NSArray        *sortDescriptors        = [NSArray arrayWithObject:
    [[NSSortDescriptor alloc] initWithKey: @"pageNumber" ascending: YES]]];

    [selectedPages: sortUsingDescriptors: sortDescriptors];

    ------------------------------------------------------------------------
    ----
    While this works

    - (id) valueForKey:(NSString *) inKey
    {
    return [pagePropertiesDict objectForKey: inKey];
    }

    NSArray        *sortDescriptors        = [NSArray arrayWithObject:
    [[NSSortDescriptor alloc] initWithKey: @"page_number" ascending: YES]]];

    [selectedPages: sortUsingDescriptors: sortDescriptors];

    I understand why the bottom works but can anyone explain why the top
    fails? Seems like it should work...

    Thanks for any help

    Ken

    On Oct 19, 2007, at 12:33 PM, Ken Tozier wrote:

    > Hi
    >
    > I have an NSMutableArray containing instances of a custom "Page"
    > class and can't get NSSortDescriptor to do anything. The page class
    > has an accessor method for the property I want to sort.
    >
    > - (id) pageNumber
    > {
    > return [pageRecord objectForKey: @"page_number"];
    > }
    >
    > In the object that contains the page array, the sort descriptor
    > array is initialized like this
    >
    > NSArray        *sortDescriptors = [NSArray arrayWithObject:
    > [[NSSortDescriptor alloc] initWithKey: @"pageNumber" ascending: YES]];
    >
    > And I try to use it like this
    >
    > NSLog(@"before sorting: %@", selectedPages);
    >
    > [selectedPages sortUsingDescriptors: sortDescriptors];
    >
    > NSLog(@"after sorting: %@", selectedPages);
    >
    > Regardless of the order in which I select pages, the before and
    > after arrays are identical. Anyone see where I'm messing up?
    >
    > Thanks for any help
    >
    > Ken
  • on 10/19/07 12:04 PM, <kentozier...> purportedly said:

    > I managed to get it working but don't know why this fails
    >
    > - (id) pageNumber
    > {
    > return [pagePropertiesDict objectForKey: @"page_number"]; // returns
    > an NSNumber
    > }
    >
    > NSArray  *sortDescriptors  = [NSArray arrayWithObject:
    > [[NSSortDescriptor alloc] initWithKey: @"pageNumber" ascending: YES]]];
    >
    > [selectedPages: sortUsingDescriptors: sortDescriptors];
    >
    > ------------------------------------------------------------------------
    > ----
    > While this works
    >
    > - (id) valueForKey:(NSString *) inKey
    > {
    > return [pagePropertiesDict objectForKey: inKey];
    > }
    >
    > NSArray  *sortDescriptors  = [NSArray arrayWithObject:
    > [[NSSortDescriptor alloc] initWithKey: @"page_number" ascending: YES]]];
    >
    > [selectedPages: sortUsingDescriptors: sortDescriptors];
    >
    > I understand why the bottom works but can anyone explain why the top
    > fails? Seems like it should work...

    Generally when KVC/KVO is involved, things rarely "just don't work". Is
    there an exception occurring that you aren't telling us, or are you possibly
    trapping exceptions and ignoring them?

    In ay case, you should probably be using the following:

    NSArray  *sortDescriptors  = [NSArray arrayWithObject:
    [[NSSortDescriptor alloc] initWithKey: @"pagePropertiesDict.page_number"
    ascending: YES]]];

    Presuming, of course, that pagePropertiesDict is a KVC compliant property.

    Best,

    Keary Suska
    Esoteritech, Inc.
    "Demystifying technology for your home or business"
previous month october 2007 next month
MTWTFSS
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31        
Go to today