2

I'm trying to sort a multidimensional array in objective-c i know that i can sort a single dimensional array using the line of code below:

NSArray *sortedArray = [someArray sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];

I can't seem to figure out how to sort a 2D array like the one below:

 ( ("SOME_URL", "SOME_STORY_TITLE", "SOME_CATEGORY"),
    ("SOME_URL", "SOME_STORY_TITLE", "SOME_CATEGORY"),
    ("SOME_URL", "SOME_STORY_TITLE", "SOME_CATEGORY") );

If someone could provide me code that would sort the array by SOME_CATEGORY it would be of great help to me.

Thanks,

Zen_Silence

1 Answer 1

5

You need to use -sortedArrayUsingFunction:context: or sortedArrayUsingFunction:context:hint:.

static NSInteger order (id a, id b, void* context) {
    NSString* catA = [a lastObject];
    NSString* catB = [b lastObject];
    return [catA caseInsensitiveCompare:catB];
}
...
NSArray* sortedArray = [someArray sortedArrayUsingFunction:order context:NULL];

Alternatively, create a custom class for your inner "array" (which is more like a record), and implement a -compareByCategory: method on it.

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

1 Comment

Worked perfectly thanks alot i was racking my brain about this for hours. Thanks for the help!

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.