sorting crash !

Discussion in 'iOS Programming' started by chhoda, Apr 19, 2012.

  1. chhoda macrumors 6502

    Oct 25, 2008
    i am trying to do a mutable array sort, it is crashing, (or may be i guess it is going on loop), what is wrong ?

    int count = [self.objArray count] ;
        for (int i = 0; i < count; i++) {
            for (int j = 0; j < count; j++) {
                if ([self averageConsumption:[self.objArray objectAtIndex:i]] > [self averageConsumption:[self.objArray objectAtIndex:j]]) {
                    NSMutableArray *arr = [self.objArray objectAtIndex:i];
                    [self.objArray replaceObjectAtIndex:i withObject:[self.objArray objectAtIndex:j]];
                    [self.objArray replaceObjectAtIndex:j withObject:arr];
  2. cMacSW macrumors regular

    Mar 20, 2006
    Hard to tell from the info given, But I'd guess that you're getting an index out of range. What does the console log say?
  3. robvas macrumors 68020

    Mar 29, 2009
    Are you sure that shouldn't be an object and not an array? What objects are in the array, numbers, strings? You also might have an issue using '>' as a comparison instead of another method.
  4. jnoxx macrumors 65816


    Dec 29, 2010
    Aartselaar // Antwerp // Belgium
    You can actually sort an Array with Apple stuff instead of writing your own loop, which does take care of it all for you, for example. you can sort an array based on a property of the object it has in it.

Share This Page