I am passing NSDictionary to my function as a parameter. I want it's key and values to be in order as I inserted. for eg. expected output is:
mutable dict:{
zKey1 = Value1;
fKey2 = Value2;
aKey3 = Value3;
}
I have tried following ways to create and set value for keys.
NSMutableDictionary *mutableDict = [[NSMutableDictionary alloc]init];
[mutableDict setObject:@"Value1" forKey:@"zKey1"];
[mutableDict setObject:@"Value2" forKey:@"fKey2"];
[mutableDict setObject:@"Value3" forKey:@"aKey3"];
NSMutableDictionary *dic2=[[NSMutableDictionary alloc]initWithObjectsAndKeys:@"1004",@"userId",@"cucumber",@"domain",@"168d5c02f ",@"token",@"1004",@"userId1",@"cucumber",@"domain1",@"168d5c02f ",@"token1", nil];
NSDictionary * dict = [NSDictionary
dictionaryWithObjects:@[@"Ravi",@"33",@"India",@"India"]
forKeys:@[@"name",@"age",@"location",@"country"]];
NSArray *sortedKeys = [[dict allKeys] sortedArrayUsingSelector: @selector(compare:)];
NSMutableArray *sortedValues = [NSMutableArray array];
for (NSString *key in sortedKeys) {
[sortedValues addObject: [dict objectForKey: key]];
}
NSString *obj1=@"1004";
NSString *obj2=@"cucumber";
NSString *obj3=@"168d5c02f";
NSString *key1=@" userId";
NSString *key2=@"domain";
NSString *key3=@"token";
NSLog(@"dict %@",dict);
NSDictionary *dict8 =[NSDictionary
dictionaryWithObjects:@[obj1,obj2,obj3]
forKeys:@[key1,key2,key3]];
But nothing has worked I am always getting output as
mutable dict:{
aKey3 = Value3;
fKey2 = Value2;
zKey1 = Value1;
}
dict8 {
domain = cucumber;
token = 168d5c02f;
userId = 1004;
}
dict {
age = 33;
country = India;
location = India;
name = Ravi;
}
dic= {
domain = cucumber;
domain1 = cucumber;
token = "168d5c02f ";
token1 = "168d5c02f ";
userId = 1004;
userId1 = 1004;
}
It is always sorting values according to alphabetical order of keys. Many people say that NSDictionary is an unsorted container. But it does gets sorted. Need help desperately. Thank you in advance.