2

Hi i have an dictionary which is been set by me and am accessing it the code is as follows.

NSMutableDictionary* filteredDictionary = [NSMutableDictionary dictionary];
    [filteredDictionary setObject:@"xcode" forKey:@"1"];
    [filteredDictionary setObject:@"ios" forKey:@"3"];
    [filteredDictionary setObject:@"ipad" forKey:@"2"];
    [filteredDictionary setObject:@"iphone" forKey:@"5"];
    [filteredDictionary setObject:@"simulator" forKey:@"4"];

   NSLog(@"%@",filteredDictionary);

current output:

{

1 = xcode;
2 = ipad;
3 = ios;
4 = simulator;
5 = iphone;    
}
but i want 
{
1 = xcode;
3 = ios;
2 = ipad;
5 = iphone;
4 = simulator;
}

i want the dictionary as i set the object in it

i dont want to make the dictionary to sort according to it

Pls Help

Thanks in advance.....

Juan Boero
  • 6,281
  • 1
  • 44
  • 62
KSR
  • 99
  • 1
  • 12

4 Answers4

1

You can't sort the keys in-place, as dictionaries are hash-tables.

You can get the key/value pairs as an array, and sort the array before showing it though:

See https://stackoverflow.com/a/4558777/15721

Community
  • 1
  • 1
Will
  • 73,905
  • 40
  • 169
  • 246
1

The NSDictionary doesn't remember the order in which you add the keys. You'll have to do it yourself. I would suggest using an NSMutableOrderedSet.

NSMutableDictionary* filteredDictionary = [NSMutableDictionary dictionary];
NSMutableOrderedSet* keyOrder = [[NSMutableOrderedSet alloc] init];

[filteredDictionary setObject:@"xcode" forKey:@"1"];
[keyOrder addObject: @"1"];
[filteredDictionary setObject:@"ios" forKey:@"3"];
[keyOrder addObject: @"3"];

// etc

Obviously this is a pain in the neck, so create yourself a new collection class

@implementation MyMutableOrderedDictionary
{
     NSMutableDictionary* filteredDictionary = [NSMutableDictionary dictionary];
     NSMutableOrderedSet* keyOrder = [[NSMutableOrderedSet alloc] init];

}

-(void) setObject: (id) object forKey: (id <NSCopying>) key
{
    [filteredDictionary setObject: object forKey: key];
    [keyOrder addObject: key]; 
} 
-(NSOrderedSet*) keys
{
    return keyOrder;
}

// Some other methods

@end

You can implement some enumeration methods by iterating over keyOrder internally or over the keys property externally.

Note I'm not subclassing NSMutableDictionary, because that can be a pain in the arse as it is a class cluster.

JeremyP
  • 84,577
  • 15
  • 123
  • 161
0

NSDictonary doesn't guarantee order when you access it. You have to use NSArray if you want to keep order of elements.

There few ideas like OrderedDictionary (http://www.cocoawithlove.com/2008/12/ordereddictionary-subclassing-cocoa.html) but it's better to just use right objects for right purposes.

Grzegorz Krukowski
  • 18,081
  • 5
  • 50
  • 71
0

NSDictonary is not mean to sort the objects and it is absolutely unimportant to consider the order of the elements. NSdictonary is a set of key-value pair where it is expected to access the value using the respective key and so the concept of index is not useful in it. Still if you want to play on indexes you should use NSArray.

Yogi
  • 3,578
  • 3
  • 35
  • 56