我店的名字作为键和分数作为值到NSDictionary
用于节省NSUserDefaults
。 然后,我想回去按分数排序的钥匙,但我似乎无法给他们只能通过串数字进行排序。 得分100,50,300,名单200,500,例如,给了我100,200,300,50,500。
可以这样做或做我需要去了解这个不同?
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:)];
有关使用keysSortedByValueUsingSelector如何(NSDictionary的)
似乎是你所需要的按在Xcode中的文档
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:)];
@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,或者是有一个更简单的方法?
非常感谢你...
-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代替。
不知道它会帮助,但你也可以保存一个NSArray中的plist; 不像一个NSDictionary(返回基本上随机顺序键),你让他们回来,你把他们进来。
我认为这个问题的最简单的方法是使用比较..
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]);
}
希望能帮助到你..