1

How to sort this kind of case?

  1. I have a NSMutableArray.
  2. My NSMutableArray can have an object or NSMutableDictionary.
  3. My NSMutableDictionary only has 1 object (an object as its object and a string as its key)
  4. Both object is the same and has attribute name

Now, what I'm struggle with is I want to sort the NSMutableArray based on the dictionary key and the object's name? (let's just ignore the object that is added to NSMutableArray)

Bunch of thanks to person can give me idea how to solve this case!

Rendy
  • 5,572
  • 15
  • 52
  • 95
  • 2
    It will be really helpful if you could give some example of what you are trying to do. Also check this http://stackoverflow.com/questions/13738082/sort-nsdictionary-by-property-of-object-stored-as-value/ – iDev Dec 21 '12 at 03:54

1 Answers1

0

Add your NSMutableArray to a custom subclass of NSObject and sort using a custom NSComparator, like so:

(NSComparator)^(id obj1, id obj2){ /* obj1 and obj2 are NSMutableArrays */
      if ([obj1 objectAtIndex:1] isKindOfClass:[NSMutableDictionary class]) {
           // sort according to the dictionary case
      } else { // [obj1 objectAtIndex:1] isKindOfClass:[NSObject class]
           // sort according to the your other, default case
      }
}

Do note that this is untested and that you'll need to fill in the bodies of the if and else statements. The comment by the else clause is just how I code and you can eliminate it if it doesn't conform to your taste.

hd1
  • 33,938
  • 5
  • 80
  • 91