0

我有一本分类过的字典。我的问题是当我循环遍历它时再次未排序。

    //sort the poiIDArray so it appears alphabetically
NSArray *sortedValues = [[self.pois allValues] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
NSMutableDictionary *orderedDictionary=[[NSMutableDictionary alloc]init];
for(NSString *valor in sortedValues){
    for(NSString *clave in [self.pois allKeys]){
        if ([valor isEqualToString:[self.pois valueForKey:clave]]) {
            [orderedDictionary setValue:valor forKey:clave];
        }
    }
}

NSLog(@"ordered dic: %@",orderedDictionary);


//loop through dic and get names. 
for(NSString *key in orderedDictionary) {

    NSLog(@"%@",key);

}

这是我的日志。

2012-10-26 11:49:35.221 CPOP Test 4[1277:c07] ordered dic: {
"Badger Burgers" = 5;
"Costa Coffee" = 4;
"Spiffing Drinks" = 6;
"Vals Vegetables" = 7;
}
2012-10-26 11:49:35.221 CPOP Test 4[1277:c07] Costa Coffee
2012-10-26 11:49:35.222 CPOP Test 4[1277:c07] Spiffing Drinks
2012-10-26 11:49:35.222 CPOP Test 4[1277:c07] Badger Burgers
2012-10-26 11:49:35.222 CPOP Test 4[1277:c07] Vals Vegetables

谁能告诉我为什么会发生这种情况以及如何解决?

4

1 回答 1

5

字典根据定义是无序的。如果您需要特定顺序,请使用数组。

于 2012-10-26T11:05:43.993 回答