NSDictionary订单与allKeys订单不匹配

时间:2014-03-02 02:32:02

标签: ios objective-c uitableview nsdictionary

我已经按照第一个字母组织的名称创建了排序数组的NSDictionary(参见下面的结果)。当我对同一个字典使用命令allKeys时,顺序不一样。我需要相同的顺序,因为这个NSDictionary在UITableview中使用,应该是按字母顺序排列的。

 - (NSDictionary*) dictionaryNames {

NSDictionary *dictionary;
NSMutableArray *objects = [[NSMutableArray alloc] init];

NSArray *letters = self.exhibitorFirstLetter;
NSArray *names = self.exhibitorName;

for (NSInteger i = 0; i < [self.exhibitorFirstLetter count]; i++)
{
    [objects addObject:[[NSMutableArray alloc] init]];
}

dictionary = [[NSDictionary alloc] initWithObjects: objects forKeys:letters];

for (NSString *name in names) { 
    NSString *firstLetter = [name substringToIndex:1];

    for (NSString *letter in letters) {  //z, b
        if ([firstLetter isEqualToString:letter]) {
            NSMutableArray *currentObjects = [dictionary objectForKey:letter];
            [currentObjects addObject:name];
        }
    }

}

    NSLog(@"%@", dictionary);
NSLog(@"%@", [dictionary allKeys]);
return dictionary;

}

 B =     (
    "Baker's Drilling",
    "Brown Drilling"
);
C =     (
    "Casper Drilling"
);
J =     (
    "J's, LLC"
);
N =     (
    "Nelson Cleaning",
    "North's Drilling"
);
T =     (
    "Tim's Trucks"
);
Z =     (
    "Zach's Main",
    "Zeb's Service",
    "Zen's"
);

}

J,
T,
B,
N,
Z,
C

2 个答案:

答案 0 :(得分:4)

NSDictionary不是有序集合。没有办法控制它的订购方式,它可能会根据操作系统版本,设备类型和字典内容完全改变。

答案 1 :(得分:1)

我只是将NSDictionary放在有序数组中:

- (NSArray*)sortAllKeys:(NSArray*)passedArray{
    NSArray* performSortOnKeys = [passedArray sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
    return performSortOnKeys;
}