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 !

iosc
  • 41
  • 1
  • 5
  • What have you thought about? What have you tried? What did it do wrong? Show the code. – Wain Aug 10 '14 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... – iosc Aug 10 '14 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 (idx1idx2) ? NSOrderedDescending : NSOrderedSame; }]; This sounds like doing the job... – iosc Aug 10 '14 at 18:39

1 Answers1

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:

Dancreek
  • 9,524
  • 1
  • 31
  • 34
  • 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. – iosc Aug 10 '14 at 15:27