sorting crash !

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

  1. chhoda macrumors 6502

    Joined:
    Oct 25, 2008
    #1
    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 ?

    Code:
    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

    Joined:
    Mar 20, 2006
    #2
    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

    Joined:
    Mar 29, 2009
    Location:
    USA
    #3
    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

    jnoxx

    Joined:
    Dec 29, 2010
    Location:
    Aartselaar // Antwerp // Belgium
    #4
    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