2009-10-25 116 views
17

我將名稱作爲鍵和值存儲到NSDictionary中以保存在NSUserDefaults中。然後我想要找回按分數排序的鍵,但我似乎無法用數字排序它們,只能通過字符串排序。例如,得分100,50,300,200,500的列表給我100,200,300,50,500.按數值排序的NSDictionary鍵

這可以做到嗎?還是我需要對此有不同的看法?

NSString *defaultNames[] = {@"Matt", @"Terry",@"Jessica",@"Sean",nil}; 
NSNumber *defaultScores[] = {@"600", @"500",@"100",@"50", nil}; 

NSDictionary *newScoreDict = [NSDictionary dictionaryWithObjects:(id *)defaultScores forKeys:(id *)defaultNames count:7]; 

NSArray *currScores = [scoreDict keysSortedByValueUsingSelector:@selector(compare:)]; 
+3

你爲什麼把字符串到您的NSNumber指針數組? – 2009-10-25 06:07:28

+0

這是一個很好的問題......嘿嘿,我一定錯過了 – Matt 2009-10-25 06:23:55

+1

爲什麼你說數組中有7個對象時,真的有4個? – 2010-03-04 17:46:39

回答

2

-compare:是一個字符串比較。通過不同的方法進行比較,例如:

@implementation NSString (numericComparison) 

- (NSComparisonResult) compareNumerically:(NSString *) other 
{ 
float myValue = [self floatValue]; 
float otherValue = [other floatValue]; 
if (myValue == otherValue) return NSOrderedSame; 
return (myValue < otherValue ? NSOrderedAscending : NSOrderedDescending); 
} 

@end 

在您的具體情況下,您可以改用-intValue。

+0

試圖編寫類似於此的代碼,但不接受NSString,而是接受一個int。什麼[self floatValue];指向? – prince 2012-06-26 20:51:11

0

不確定這會有幫助,但你也可以將一個NSArray保存在plist中;不像一個NSDictionary(它返回基本上是隨機的順序鍵),你讓他們回來,你把他們進來。

7

如何使用keysSortedByValueUsingSelector(NSDictionary中)

好像是你所需要的按在Xcode中的文檔

+0

只適用於10.6+ – hmak 2011-04-20 05:21:13

+2

@hmak:不可以。您一定是在考慮'keysSortedByValueUsingComparator:'帖子提到'keysSortedByValueUsingSelector:',它已經可用10.0 – user102008 2011-10-09 07:30:51

+1

我應該提到的問題是iOS不是MacOS? – 2012-10-23 15:41:37

3
@implementation NSString (numericComparison) 

- (NSComparisonResult) floatCompare:(NSString *) other 
{ 
    float myValue = [self floatValue]; 
    float otherValue = [other floatValue]; 
    if (myValue == otherValue) return NSOrderedSame; 
    return (myValue < otherValue ? NSOrderedAscending : NSOrderedDescending); 
} 

- (NSComparisonResult) intCompare:(NSString *) other 
{ 
    int myValue = [self intValue]; 
    int otherValue = [other intValue]; 
    if (myValue == otherValue) return NSOrderedSame; 
    return (myValue < otherValue ? NSOrderedAscending : NSOrderedDescending); 
} 

@end 

NSString *defaultNames[] = {@"Matt", @"Terry",@"Jessica",@"Sean",nil}; 
// NSNumber *defaultScores[] = {@"600", @"500",@"100",@"50", nil}; 

NSNumber *defaultScores[] = {                 
      [NSNumber numberWithInt:600], 
      [NSNumber numberWithInt:500], 
      [NSNumber numberWithInt:100], 
      [NSNumber numberWithInt:50], 
      nil 
    }; 

NSDictionary *newScoreDict = [NSDictionary dictionaryWithObjects:(id *)defaultScores forKeys:(id *)defaultNames count:4]; 

NSArray *currScores = [newScoreDict keysSortedByValueUsingSelector:@selector(intCompare:NotSureWhatGoesHere:)]; 

我仍然與前面的線路混淆?

難道我只是用

// 
NSArray *currScores = [newScoreDict keysSortedByValueUsingSelector:@selector(intCompare:other:)]; 
// 

是一個數字OK的陣列,或者是有一個更簡單的方法?

非常感謝你......

+0

爲什麼你使用float而不是double? – gnasher729 2014-05-30 14:53:31

5
NSString *defaultNames[] = {@"Matt", @"Terry",@"Jessica",@"Sean",nil}; 
NSNumber *defaultScores[] = {@"600", @"500",@"100",@"50", nil}; 
NSDictionary *newScoreDict = [NSDictionary dictionaryWithObjects:(id *)defaultScores forKeys:(id *)defaultNames count:7]; 
NSArray *currScores = [scoreDict keysSortedByValueUsingSelector:@selector(localizedStandardCompare:)]; 
+0

我無法得到這個工作,因爲我的值是NSNumbers。但我使用這個比較器,它的工作原理:https://stackoverflow.com/a/9708772/2057171 – 2017-10-26 21:21:56

0

我認爲這個問題的最簡單的方法是使用比較..

NSString *defaultNames[] = {@"Matt", @"Terry",@"Jessica",@"Sean",nil}; 
NSNumber *defaultScores[] = {@(600), @(500),@(400),@(50), nil}; 
NSDictionary *newScoreDict = [NSDictionary dictionaryWithObjects:defaultNames  forKeys:defaultScores count:4]; 
NSArray *currScores = [newScoreDict keysSortedByValueUsingComparator:^NSComparisonResult(id obj1, id obj2) { 
    if ([obj1 integerValue] > [obj2 integerValue]) { 
     return NSOrderedAscending; 
    }else{ 
     return NSOrderedDescending; 
    }}]; 

for (NSString *string in currScores) { 
    NSLog(@"%@",string); 
} 

試試這個.. 我注意到,我不能達到與價值使用NSNumber對象,所以如果你想達到的對象的價值比我更改你的NSNumber得分爲NSString並將其轉換爲數字,而訂購時解決。您可以使用如下..

NSString *defaultNames[] = {@"Matt", @"Terry",@"Jessica",@"Sean",nil}; 
NSString *defaultScores[] = {@"600", @"500",@"400",@"50", nil}; 
NSMutableDictionary *newScoreDict = [NSMutableDictionary dictionaryWithObjects:defaultScores forKeys:defaultNames count:4]; 

NSArray *currScores = [newScoreDict keysSortedByValueUsingComparator:^NSComparisonResult(id obj1, id obj2) { 
NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init]; 
[formatter setNumberStyle:NSNumberFormatterDecimalStyle]; 
NSNumber *number1 = [formatter numberFromString:obj1]; 
NSNumber *number2 = [formatter numberFromString:obj2]; 
    if (number1.intValue > number2.intValue) { 
     return NSOrderedDescending; 
    }else{ 
     return NSOrderedAscending; 
    }}]; 

for (NSString *name in currScores) { 
    NSLog(@"key %@ value %@",name,[newScoreDict valueForKey:name]); 
} 

希望它可以幫助..