我有一個Person NSDictionary,其關鍵是人的名稱,而對象是一個帶有兩個鍵的NSDictionary:他的暱稱(NSString)和他的年齡(NSNumber)。在嵌套NSDictionary中查找最小值和最大值
我想結束按照他們年齡的升序排序的人字典,這樣我就可以得到最年輕和最老的人的名字。 做什麼是最好的方法?
謝謝!
我有一個Person NSDictionary,其關鍵是人的名稱,而對象是一個帶有兩個鍵的NSDictionary:他的暱稱(NSString)和他的年齡(NSNumber)。在嵌套NSDictionary中查找最小值和最大值
我想結束按照他們年齡的升序排序的人字典,這樣我就可以得到最年輕和最老的人的名字。 做什麼是最好的方法?
謝謝!
有些語言提供有序字典,但標準NSDictionary
固有地未排序。您可以獲取所有鍵,對鍵列進行排序,然後根據排序的鍵遍歷詞典。 (NSDictionary
有幾個方便的方法,我不知道這個用例,請參閱Anurag的回答。)
你的情況有點複雜,解決它的一種方法是引入一個臨時字典映射年齡到名稱。但是如果你只最小和最大年齡後的時候,只是遍歷所有的人,並保持最大&最低年齡和名字的軌跡:
NSString *oldestName = nil;
float maxAge = -1;
for (NSString *name in [persons allKeys]) {
NSDictionary *info = persons[name];
float age = [info[@"age"] floatValue];
if (age > maxAge) {
oldestName = info[@"nick"];
maxAge = age;
}
}
如果我們回到整理字典的想法,這可以工作:
NSArray *peopleByAge = [people keysSortedByValueUsingComparator:^(id a, id b) {
// Again, see Anurag’s answer for a more concise
// solution using the compare: method on NSNumbers.
float ageA = [a objectForKey:@"age"];
float ageB = [b objectForKey:@"age"];
return (ageA > ageB) ? NSOrderedDescending
: (ageB > ageA) ? NSOrderedAscending
: NSOrderedSame;
}];
有一些方便的方法在NSDictionary中定義按值排序項並獲取排序的鍵。
見文檔,
keysSortedByValueUsingComparator:
keysSortedByValueUsingSelector:
keysSortedByValueWithOptions:usingComparator:
我猜你正在使用的現代Objective-C的語法和年齡其實是代表數字。下面是它的外觀:
[people keysSortedByValueUsingComparator:(NSDictionary *firstPerson, NSDictionary *secondPerson) {
return [firstPerson[@"age"] compare:secondPerson[@"age"]];
}];
謝謝!您的答案也有效..不幸的是,不可能將兩個答案標記爲已接受:) –
As @Zoul表示標準NSDictionary是未排序的。
排序,你可以使用一個數組,我做這樣的事情
//the dictionary is called dict : in my case it is loaded from a plist file
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
//make a dicoArray that is sorted so the results are sorted
NSArray *dicoArray = [[dict allKeys] sortedArrayUsingComparator:^(id firstObject, id secondObject) {
return [((NSString *)firstObject) compare:((NSString *)secondObject) options:NSNumericSearch];
}];
檢查所有排序選項的幫助。在呈現的案例中,字典被按鍵處理爲數字值(這對我來說是這樣)。
如果需要排序的另一種方式的可能性排序名單是
enum {
NSCaseInsensitiveSearch = 1,
NSLiteralSearch = 2,
NSBackwardsSearch = 4,
NSAnchoredSearch = 8,
NSNumericSearch = 64,
NSDiacriticInsensitiveSearch = 128,
NSWidthInsensitiveSearch = 256,
NSForcedOrderingSearch = 512,
NSRegularExpressionSearch = 1024
};
看,它返回一個選擇排序鍵NSDictionary's method。有不止一種這樣的方法。你得到一排排序的密鑰,然後訪問第一個和最後一個,並擁有你最年輕和最老的人。
在iOS 9。2
// NSNumbers的字典
NSDictionary * phoneNumbersDict = @{@"400-234-090":67,@"701-080-080":150};
//以升序
NSArray * keysArraySortedByValue = [phoneNumbersDict keysSortedByValueUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
return [obj1 compare:obj2];
}];
//降序
NSArray * keysArraySortedByValue = [phoneNumbersDict keysSortedByValueUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
return [obj2 compare:obj1];
}];
這裏是NSComparisonResults的枚舉。
enum {
NSOrderedAscending = -1,
NSOrderedSame,
NSOrderedDescending
};
typedef NSInteger NSComparisonResult;
你的意思是隻是手動排序呢?沒有任何方便的方法可以使用自定義比較器嗎? –
'NSArray'有幾種排序方法,但請參閱我的編輯 - 對於您的情況,最好忘記排序。 – zoul
非常感謝! –