2011-02-28 62 views
4

在我找到的教程中使用NSDictionary的Im,但我只是有一個簡單的問題,爲什麼它按字母順序排序?爲什麼我的NSDictionary鍵按字母順序排序

NSArray *array = [[NSArray alloc] initWithObjects:userName, nil]; 
    NSArray *array2 = [[NSArray alloc] initWithObjects:@"Other Data", @"Second Entry", nil]; 

    NSDictionary *temp =[[NSDictionary alloc] initWithObjectsAndKeys:array, @"Name", array2, @"A",nil]; 

    self.sortedKeys =[[temp allKeys] sortedArrayUsingSelector:@selector(compare:)]; 

感謝您的幫助。

回答

18

因爲你是按字母順序排序嗎?

self.sortedKeys =[[temp allKeys] sortedArrayUsingSelector:@selector(compare:)]; 

需要注意的是一本字典的鍵是固有無序。如果您需要對鍵應用某種排序順序,則需要將其與字典分開進行維護。

+1

如果我只是想讓他們按照輸入排序我怎麼能這樣做? – 2011-02-28 19:58:04

+1

有沒有辦法使標準的'NSDictionary'保持鍵的順序,看到這[相關問題](http://stackoverflow.com/questions/376090)。 – zoul 2011-02-28 21:00:14