0

I'm trying to sort a NSMutableArray by a NSArray content.

For example, the content of NSMutableArray (which contains NSMutableDictionnary) is :

{
    detail = "Al's legend text";
    image = "http://...";
    main = "Al's main text;
    section = "United States";
},
{
    detail = "Yumi's legend text";
    image = "http://...";
    main = "Yumi's main text;
    section = "Japan";
},
{
    detail = "Enrico's legend text";
    image = "http://...";
    main = "Enrico's main text;
    section = "Spain";
}, ...

For now, I know how to sort it by key, ascending or descending but what I need is the NSMutableArray be sorted by the following NSArray content :

{ @"Japan",@"France",@"United States",@"Uruguay",@"Spain", ...}

If someone can help me to figure it out. Big thanks !

3
  • What have you thought about? What have you tried? What did it do wrong? Show the code. Commented Aug 10, 2014 at 14:39
  • The fact is I just didn't find any solution to sort a NSArray by comparing key (@"section") with a NSArray object... Commented Aug 10, 2014 at 14:44
  • [tableViewArray sortUsingComparator:^NSComparisonResult(id obj1, id obj2) { NSUInteger idx1 = [sectionOrderSort indexOfObject:[obj1 valueForKey:@"section"]]; NSUInteger idx2 = [sectionOrderSort indexOfObject:[obj2 valueForKey:@"section"]]; return (idx1<idx2) ? NSOrderedAscending : (idx1>idx2) ? NSOrderedDescending : NSOrderedSame; }]; This sounds like doing the job... Commented Aug 10, 2014 at 18:39

1 Answer 1

0

You want to use sortUsingSelector: this method of nsmutablearray array requires that you write a function that does your custom comparison. Look at the documentation and it will show you how to use it.

https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSMutableArray_Class/Reference/Reference.html#//apple_ref/occ/instm/NSMutableArray/sortUsingSelector:

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

1 Comment

Thanks Dancreek, but I'm a beginner in obj-c, and this is not helping me - Can't know how to code it in order to make the comparaison between the key and the nsarray order.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.