1

I want to sort a NSMutableArray which has NSArrays with respect to the value at the index 1 of the NSArray. I'll try to draw a picture of arrays.

NSMutableArray *ultimateHighscoreArray = { 
    ( (NSString) userName, (double) score, (int) numOfCorrectAnswers ) , 
      ( John , 4.5 , 3 ) , 
      ( Terry , 7.5 , 1) , 
                                     ... }

The first array within the NSMutableArray is an example which shows how examples are located. Second and third are how the values actually are. So, what I want is to sort these arrays having the array containing higher value at first index to go higher up in the ranking. For this example the array that has 7.5 which is Terry Array should go before the one that has 4.5 . I want the results to be held in a NSMutableArray in decreasing order. Thank you.

2
  • I have a feeling that your array should contain instances of a custom class, or at the very least NSDictionary... Commented Jun 20, 2012 at 14:05
  • While I agree that perhaps you should restructure your data I've posted an answer for your current structure. Commented Jun 20, 2012 at 14:06

3 Answers 3

2

This should do it:

[ultimateHighscoreArray sortUsingComparator:^(id obj1, id obj2) {
    NSNumber *score1 = [obj1 objectAtIndex:1];
    NSNumber *score2 = [obj2 objectAtIndex:2];

    // Reverse the comparison here (compare score2 to score1)
    // in order to get a descending order
    return [score2 compare:score1];
}];

A general advice: your data structure would be clearer if it were an array of NSDictionary instances or even an array of custom objects (e.g., your custom Score class). In those cases, you could also use NSSortDescriptor to sort the array, which would result in cleaner, easier-to-read code.

Sign up to request clarification or add additional context in comments.

3 Comments

Your answer is nearly perfect. It worked to some point. Most of them are in the right order however some of them are not. This is weird. Do you have any guess what went wrong?
Can you give an example of the records that did not sort in the correct order?
Nevermind :) The error was most likely because of corrupt data from earlier experiments. I deleted the memory and now it sort perfectly. If I see another error like that I'll post it. Thank you! :)
2

The sortUsingComparator: method allows you to sort an array using a block, e.g.:

[ultimateHighscoreArray sortUsingComparator:^(id obj1, id obj2) {
    return [[obj1 objectAtIndex:1] compare:[obj2 objectAtIndex:1]];
}];

For clarity, it would probably be better to use an array of dictionaries (or instances of a custom class) for this data structure.

1 Comment

Note that this will sort the array in ascending order of scores.
0

You can write a custom comparator like this :)

[ultimateHighscoreArray sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
    NSArray *arrayA = (NSArray *)obj1;
    NSArray *arrayB = (NSArray *)obj2;

    double scoreA = [arrayA objectAtIndex:1];
    double scoreB = [arrayB objectAtIndex:1];

    if (scoreA > scoreB) {
        return NSOrderedDescending;
    } else if (scoreA < scoreB) {
        return NSOrderedAscending;
    } else {
        return NSOrderedSame;
    }
}];

2 Comments

This won't work. The array doesn't contain doubles but NSNumber instances.
Then just swap around the NSNumbers... I think the code is pretty straight forward to be edited.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.