散景。這裏是協議: 有一個NSMutualDictionary與單詞作爲鍵(說名字)。值對象是一個NSNumber(如評級)按對象排序NSMutableDictionary鍵?
NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] init];
[dictionary setObject:[NSNumber intValue:1] forKey:@"Melvin"];
[dictionary setObject:[NSNumber intValue:2] forKey:@"John"];
[dictionary setObject:[NSNumber intValue:3] forKey:@"Esben"];
我想按照最高收視率先排序。
我知道我會做這樣的:
[searchWords keysSortedByValueUsingSelector:@selector(intCompare:)];
,但不知道如何實現intCompare。 (比較方法)
任何人都可以指向正確的方向嗎?
- (NSComparisonResult) intCompare:(NSString *) other
{
//What to do here?
}
我想用{Esben,John,Melvin}得到一個NSArray。
是的,tjats正確。我的印象是隻有NSString比較:實施。但是因爲NSNumber也應該這樣做。現在我該如何設置A的結尾或下降方向? – esbenr
但還有一個問題。使用選擇器(比較:)按對象對字典進行排序,但先將最低值排序。我如何改變他的排序方向? – esbenr
@esbenr - 對此沒有直接的支持。但是你可以使用' - [NSDictionary keysSortedByValueUsingComparator:]'來做你自己的反轉比較。 – PeyloW