0

I need so sort an array with an array inside, something like this:

 NSMutableArray * array_example = [[NSMutableArray alloc] init];

 [array_example addObject:[NSMutableArray arrayWithObjects:
                            string_1,
                            string_2,
                            string_3,
                            nil]
 ];

how can i order this array by the field "string_1" of the array??? any idea how can i do that?

Thanks

1
  • Watch out for the missing pointer for array_example, and the extra comma in the arrayWithObjects declaration, please refer to my answer below. Good luck Commented Aug 22, 2011 at 10:15

2 Answers 2

1

For iOS 4 and later, this is easily done using a comparator block:

[array_example sortUsingComparator:^(NSArray *o1, NSArray *o2) {
    return (NSComparisonResult)[[o1 objectAtIndex:0] compare:[o2 objectAtIndex:0]];
}];

If you're interested in how blocks work, you can have a look at Apple's Short Practical Guide to Blocks.

If you wish to support iOS 3.x, you'd have to use a custom comparison function:

NSComparisonResult compareArrayFirstElement(NSArray *o1, NSArray *o2) {
    return [[o1 objectAtIndex:0] compare:[o2 objectAtIndex:0]];
}

and then use:

[array_example sortUsingFunction:compareArrayFirstElement context:nil];
Sign up to request clarification or add additional context in comments.

3 Comments

for your first example i need to do that block for each item of the array_example like: for(int i=0; i<[array_example count]; i++){...} i don t understand what will be the array "o1" and o2"
No you just have to use the code as it is written. I've added a link to Apple docs if you want more understanding of blocks. NSMutableArray reference doc can help you too.
Use [[o2 objectAtIndex:0] compare:[o1 objectAtIndex:0]] instead
1

You can loop the array objects and call sortedArrayUsingSelector on each sub array, then replaceObjectAtIndex:withObject to inject back into the original array

    NSMutableArray * array_example = [[NSMutableArray alloc] init];

    [array_example addObject:[NSMutableArray arrayWithObjects:
                            @"z",
                            @"a",
                            @"ddd",
                            nil]
    ];
    [array_example addObject:[NSMutableArray arrayWithObjects:
                            @"g",
                            @"a",
                            @"p",
                            nil]
    ];
    NSLog(@"Original Array: %@", array_example);

    for(int i = 0; i < [array_example  count] ; i++){
        [array_example replaceObjectAtIndex:i withObject:[[array_example objectAtIndex:i] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)]];
        // order sub array
    }
    NSLog(@"Sorted Array: %@", array_example);

Comments

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.