6

I am storing data in NSMutableDictionary To store data received by web service.

I want to display data in alpha order but as we know Dictionary store data in their own way so I m getting different order for iOS 5 and iOS 6. iOS 5 it is fine but in iOS 6 it change order.

I am storing int key and object as value.

object contain ID,Name and Description.

EX:
  key  value
  1 -  (12,bhgjksd,kijdh)
  6 -  (13,kjfd,ikjk)
  2 -  (14,ljiuhbdf,dsjhfj)

How can I sort my Dictionary by Object.Name property. or get key based on Object.Name sorting order.

I don't want to sort on key I want to sort on Object.Name which is stored in value part as object.

Any kind of help is welcome..... Thanks in advance..

iDev
  • 23,310
  • 7
  • 60
  • 85
CRDave
  • 9,279
  • 5
  • 41
  • 59
  • You can have a look at this http://stackoverflow.com/questions/8118932/how-can-i-sort-nsmutabledictionary-with-keys-value – amrit_neo Dec 06 '12 at 06:29
  • 1
    hi Amrit thanks to have time. But in that question he want sorting based on key while i want sorting based on value. – CRDave Dec 06 '12 at 06:34

2 Answers2

6

Try this,

NSArray *sortedKeys = [dict keysSortedByValueUsingComparator: ^(id obj1, id obj2) {

     return (NSComparisonResult)[obj1.name compare:obj2.name];;
}];

You will get all the keys in sortedKeys array, sorted in the order of object.name property. You can use these keys to get the values from dictionary as [dict valueForKey:[sortedKeys objectAtIndex:i]]. For more details please check apple documentation.

iDev
  • 23,310
  • 7
  • 60
  • 85
  • thanks This solved my problem I fond it from keysSortedByValueUsingComparator. First I found it from here http://stackoverflow.com/questions/11217421/sort-nsdictionary-keys-by-dictionary-value-into-an-nsarray?rq=1. Thanks to try help me. – CRDave Dec 06 '12 at 07:06
  • This also help me to campare NSString http://getsetgames.com/2009/12/10/iphonedev-advent-tip-10-string-comparison-using-nsstring/ – CRDave Dec 06 '12 at 07:13
  • I was not aware that I can accept only one answer SO i go for select another answer. – CRDave Dec 06 '12 at 07:14
3
NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"Object.Name" ascending:YES];
[items sortUsingDescriptors:[NSArray arrayWithObjects:descriptor,nil]];

Also,

NSArray *ordered = [myDictionary keysSortedByValueUsingComparator:^NSComparisonResult(id obj1, id obj2){
    return [obj1.name compare:obj2.name];
}];
Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
  • Thanks to try help me. I solved my problem by keysSortedByValueUsingComparator – CRDave Dec 06 '12 at 07:06
  • 1
    This also help me to campare NSString http://getsetgames.com/2009/12/10/iphonedev-advent-tip-10-string-comparison-using-nsstring/ – CRDave Dec 06 '12 at 07:09
  • I did not copy from you, I just posted answer in 2-ways. And its upto the OP, which way he wants. Even there is some differences in your and my answer. I dont care if someone accepts my answer or not. Even a downvote will be fine for me ;) – Anoop Vaidya Dec 06 '12 at 07:17