0

I have NSMutableDictionary with several keys (NSDate is the key) and value of each key is NSMutableArray whose objects are dictionaries. Each of this dictionary has NSDate "event_start_date" based on which I want to sort NSMutableArray. How can I achieve it? Here's the format of NSMutableDictionary that I've.

{
    "2015-03-23 18:30:00 +0000" =     (
                {
            "establishment_id" = 8;
            "event_end_date" = "2015-03-24 20:15:00";
            "event_image" = "";
            "event_name" = "Event One";
            "event_start_date" = "2015-03-24 19:15:00";
            miles = "0.17";
            "user_rate" = 0;
        }
    );
    "2015-03-24 18:30:00 +0000" =     (
                {
            "establishment_id" = 8;
            "event_end_date" = "2015-03-25 20:20:00";
            "event_image" = "";
            "event_name" = "Second Event";
            "event_start_date" = "2015-03-25 19:20:00";
            miles = "0.17";
            "user_rate" = 0;
        },
                {
            "establishment_id" = 8;
            "event_end_date" = "2015-03-30 01:00:00";
            "event_image" = "";
            "event_name" = getevent;
            "event_start_date" = "2015-03-25 01:00:00";
            miles = "0.17";
            "user_rate" = 0;
        },
                {
            "establishment_id" = 8;
            "event_end_date" = "2015-03-25 15:40:00";
            "event_image" = "";
            "event_name" = "TestAdd Event with note";
            "event_start_date" = "2015-03-25 14:40:00";
            miles = "0.17";
            "user_rate" = 0;
        }
    );
    "2015-03-26 18:30:00 +0000" =     (
                {
            "establishment_id" = 8;
            "event_end_date" = "2015-03-27 11:25:00";
            "event_image" = "http://venyounightout.com/images/1_2015_03_26_23_00_00.png";
            "event_name" = "Perfect event";
            "event_start_date" = "2015-03-27 10:25:00";
            miles = "0.17";
            "user_rate" = 0;
        }
    );
}
3
  • There's a lot of existing questions about sorting. Have you done any searching? Have you looked at the docs for NSArray/NSMutableArray and the sorting related methods? Update your question with what you have tried.
    – rmaddy
    Mar 27, 2015 at 4:25
  • @rmaddy, After searching in google etc, I understood that individually sorting array or keys of dictionary based on values etc are available. The other possibility is iterate through each key, get the array, sort it and replace previous value of key with new sorted array etc. But I don't think this is better way and might impact performance. I am looking for optimised way to achieve it.
    – Satyam
    Mar 27, 2015 at 4:30
  • Since the arrays are mutable, you don't have to create a new sorted array and replace the previous value. You can just sort the mutable array in place, which is what my answer does. If you want the arrays to be sorted, how do you expect to optimize away the sorting of the arrays? Mar 28, 2015 at 2:12

2 Answers 2

0

you may not sort an NSDictionary - you may sort an array you get from the dictionary however.

// get all keys into array
NSArray * keys = [your_dictionary allKeys];

// sort it
NSArray * sorted_keys = [keys sortedArrayUsingSelector:@selector(compare:)];

// now, access the values in order
for (NSDate * key in sorted_keys)
 {
   // get value
   NSMutableString * your_value = [your_dictionary valueForKey: key];
   // perform operations
}
1
  • He's not trying to sort the keys or access the values by the keys in sorted order. He's trying to sort the mutable arrays which are the values in the dictionary. Mar 27, 2015 at 7:32
0

You can sort an individual NSMutableArray of dictionaries by the "event_start_date" key using:

[array sortUsingDescriptors:@[ [NSSortDescriptor sortDescriptorWithKey:@"event_start_date" ascending:YES] ]];

To apply this to every value in a dictionary, you can do:

[dict enumerateKeysAndObjectsUsingBlock:^(id key, NSMutableArray* array, BOOL *stop){
    [array sortUsingDescriptors:@[ [NSSortDescriptor sortDescriptorWithKey:@"event_start_date" ascending:YES] ]];
}];
2
  • If its just one array, this works good. I've to repeat the same for all the values of the dictionary which are basically arrays. Apart from getting all the keys and iterating over them for each value and then sort is not good I think.
    – Satyam
    Mar 27, 2015 at 18:13
  • I don't understand your comment. I showed code for sorting all of the values of the dictionary, which is what you asked for I think. Mar 28, 2015 at 2:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.