Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Prevent NSMutableDictionary being sorted

AnanthAnanth Posts: 51New Users
edited May 2012 in iPhone SDK Development
Hi all,
Im in need of getting the keyValues of an NSMutableDictionary as in the order it is.
I tried to get them using

[myDict allKeys];


Also, I tried out
NSEnumerator *enumerator = [myDict keyEnumerator];
        for (NSString * strng in enumerator)
       {
            NSLog(@"%@", strng);
        }


In both the above methods, I got dictionary key values sorted alphabetically. I don't want the keys to get alphabetically sorted and want as in the order it was. Please some one direct me to get it done.
Thanks in advance.
Post edited by Ananth on

Replies

  • Duncan CDuncan C Posts: 9,022Tutorial Authors, Registered Users @ @ @ @ @ @ @
    edited April 2012
    Ananth wrote: »
    Hi all,
    Im in need of getting the keyValues of an NSMutableDictionary as in the order it is.
    I tried to get them using

    [myDict allKeys];
    


    Also, I tried out
    NSEnumerator *enumerator = [myDict keyEnumerator];
            for (NSString * strng in enumerator)
           {
                NSLog(@"%@", strng);
            }
    


    In both the above methods, I got dictionary key values sorted alphabetically. I don't want the keys to get alphabetically sorted and want as in the order it was. Please some one direct me to get it done.
    Thanks in advance.

    Dictionaries are unordered collections. There is no "the order it is." You might get one order of keys one time, and a different order the next time.
    Regards,
    Duncan C
    WareTo

    widehead.gif
    Animated GIF created with Face Dancer, available for free in the app store.

    I'm available for one-on-one help at CodeMentor
  • AnanthAnanth Posts: 51New Users
    edited April 2012
    Duncan C wrote: »
    Dictionaries are unordered collections. There is no "the order it is." You might get one order of keys one time, and a different order the next time.

    So, if need to get ordered content, I should not use NSMutableDictionary. Think I need to get it done through NSMutableArray. Hope Am right.
  • Duncan CDuncan C Posts: 9,022Tutorial Authors, Registered Users @ @ @ @ @ @ @
    edited April 2012
    Ananth wrote: »
    So, if need to get ordered content, I should not use NSMutableDictionary. Think I need to get it done through NSMutableArray. Hope Am right.

    With dictionaries, you can sort the keys and then fetch the data using those keys in sorted order.

    If you want a container that has an innate ordering of the data, you want an array.
    Regards,
    Duncan C
    WareTo

    widehead.gif
    Animated GIF created with Face Dancer, available for free in the app store.

    I'm available for one-on-one help at CodeMentor
  • AnanthAnanth Posts: 51New Users
    edited May 2012
    Duncan C wrote: »
    With dictionaries, you can sort the keys and then fetch the data using those keys in sorted order.

    If you want a container that has an innate ordering of the data, you want an array.

    Thats a good idea. Thanks for your response @Duncan C. Will try it out.
Sign In or Register to comment.