Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

chhoda

macrumors 6502
Original poster
Oct 25, 2008
285
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];
            }
        }
    }
 
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?
 
Code:
NSMutableArray *arr = [self.objArray objectAtIndex:i];

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.
 
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.