1

I'm trying to sort an array, from a previous post I was pointed to an answer in this thread, Sorting an array of doubles or CLLocationDistance values on the iPhone

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

Community
  • 1
  • 1
Stephen
  • 4,715
  • 8
  • 53
  • 78

2 Answers2

1
  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];

iOSPawan
  • 2,884
  • 2
  • 25
  • 50
  • (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 Sep 17 '10 at 11:40
  • Anybody any thoughts on this, I'm still having trouble. – Stephen Sep 20 '10 at 14:47
1

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

Like this...

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

Hope that helps.

Firoze Lafeer
  • 17,133
  • 4
  • 54
  • 48
  • 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 Sep 23 '10 at 08:57