tags:

views:

200

answers:

3

O have an NSDictionary. Inside the dictionary there is an array. I want to sort the dictionary and display it's value in ascending order so please any one help me how can I get sorted data?

+3  A: 

There are three function of NSDictionary...

 – keysSortedByValueUsingSelector:
 – keysSortedByValueUsingComparator:
 – keysSortedByValueWithOptions:usingComparator:
mihirpmehta
if you don't mind please tell me with example i want to sorted by "rname".Please help me..NSDictionary *dict = (NSDictionary *)[DataBaseAccess getRestaurantDetails]; NSLog(@"%@",dict);2010-04-23 11:45:07.089 Restaurant[2074:207] ( { raddress = "Cascal,Mountain View,CA."; rdistance = "7.96 miles"; rlatitude = "37.391167"; rlongitude = "-122.080974"; rname = Cascal; rphno = 6509409500; } )
Ankit Vyas
This is only one Value of dictionary i want to sort by name all values and i have to display so how i can use this function..
Ankit Vyas
do you mean you have more than one dictionaries and you wont to sort it...? do you want to sort dictionaries or data inside dictionaries...?
mihirpmehta
Data inside the dictionaries....
Ankit Vyas
Sorting by "rname". All data should be arranged by rname accordingly.
Ankit Vyas
yes but you have NSDictionary *dict for every Record right...?
mihirpmehta
Yes i have dictionary and in dictionary i have values..
Ankit Vyas
you have to take array of NSDictionaries and sort it by NSArray's sorting function...
mihirpmehta
i have already tried it but by using that my application is terminated.
Ankit Vyas
A: 

Check out this link: http://bravobug.com/news/?p=323

Kenny
A: 

Use NSSortDescriptor like this..

NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"name"  ascending:YES];
[stories sortUsingDescriptors:[NSArray arrayWithObjects:descriptor,nil]];
recent = [stories copy];

Stories is the array you want to sort .recent is another mutable array which has sorted dictionary values.change the "name" with the key value on which you have to sort.

All the best

Warrior