views:

56

answers:

2

Hello,

I'm trying to sort an array, from a previous post I was pointed to an answer in this thread, http://stackoverflow.com/questions/1422840/sorting-an-array-of-doubles-or-cllocationdistance-values-on-the-iphone/1422881#1422881

Based of this my code is as follows:

NSArray *sortedArray = [listArray sortedArrayUsingFunction:intSort context:nil];


NSInteger intSort(id num1, id num2, void *dummy)
{
    int v1 = [[num1 objectForKey:@"waypoint_order"] intValue];
    int v2 = [[num2 objectForKey:@"waypoint_order"] intValue];

    if (v1 < v2)
        return NSOrderedAscending;
    else if (v1 > v2)
        return NSOrderedDescending;
    else
        return NSOrderedSame;
}

But its crashing on line int v1 = [[num1 objectForKey:@"waypoint_order"] intValue]; with 'objc_exception_throw'.

What am I doing wrong, I must be leaving out some functionality.

Regards, Stephen

+1  A: 
  1. check num1 dictionary. is it autorelease or u release it somewhere in program.

  2. or check value return by Dictionary may be it returning nil;

  3. see linkArray object .at the time of creating u assigned last Value to nil or not . eg:[[NSArray alloc]initWithObject:object1,object2,object3,nil];

pawan.mangal
(1) Definitely not auto released and I'm not releasing it anywhere. (2) How do I do this ? (3) listArray is created from a NSFetchRequest and definitely no 'nil' value at the end.
Stephen
Anybody any thoughts on this, I'm still having trouble.
Stephen
+1  A: 

If these are NSManagedObjects, you want valueForKey:, not objectForKey:

Like this...

int v1 = [[num1 valueForKey:@"waypoint_order"] intValue];

Hope that helps.

Firoze Lafeer
Firoze, thanks very much for this, I can't believe I missed it. I should have known that. I've been working a lot with NSManagedObjects lately.
Stephen