我正在开发游戏应用程序,在该应用程序中我将名称、分数存储在 nsuserdefaults 中......但是 根据我的要求,我想根据分数存储名称和分数(从高到低)....是否有任何解决方案可以在 nsuserdefaults 中对分数进行排序...我想在顶部显示我的高分
感谢在广告中
-(void)btnSaveScore { if(!dictWinData) dictWinData = [[NSMutableDictionary alloc] init];
array = [[NSMutableArray alloc] init];
array = [[[NSUserDefaults standardUserDefaults] valueForKey:@"ScoreName"] mutableCopy];
if([array count] == 0)
{
array = [[NSMutableArray alloc] init];
}
NSString *strName = [NSString stringWithFormat:@"%@",strNameOFPlayer];
NSString *strScore = [NSString stringWithFormat:@"%@",[NSString stringWithFormat:@"%d",iTap]];
if([strNameOFPlayer length]==7)
[array addObject:[NSString stringWithFormat:@"%@ %@",strName,strScore]];
else if ([strNameOFPlayer length] == 6)
[array addObject:[NSString stringWithFormat:@"%@ %@",strName,strScore]];
else if ([strNameOFPlayer length] == 5)
[array addObject:[NSString stringWithFormat:@"%@ %@",strName,strScore]];
else if ([strNameOFPlayer length] == 4)
[array addObject:[NSString stringWithFormat:@"%@ %@",strName,strScore]];
else if ([strNameOFPlayer length] == 3)
[array addObject:[NSString stringWithFormat:@"%@ %@",strName,strScore]];
else if ([strNameOFPlayer length] == 2)
[array addObject:[NSString stringWithFormat:@"%@ %@",strName,strScore]];
else if ([strNameOFPlayer length] == 1)
[array addObject:[NSString stringWithFormat:@"%@ %@",strName,strScore]];
NSUserDefaults *dfltsData = [NSUserDefaults standardUserDefaults];
[dfltsData setObject:array forKey:@"ScoreName"];
[dfltsData synchronize];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert"
message:@"Score is saved."
delegate:self
cancelButtonTitle:@"Ok"
otherButtonTitles:nil, nil];
[alert show];
[alert release];
请帮帮我
最佳答案
从 NSUserDefault 获取数组,如下所示:
NSMutableArray *arrScores = [[NSUserDefaults standardUserDefaults] ObjectforKey:@"ScoreName"];
现在排序 arrScores 应该只包含 strScore 值仅使用以下方法:
-(NSMutableArray *)sortByfloatvalue:(NSMutableArray *)array
{
for(int i=0;i<[array count];i++)
{
for(int j=i+1;j<[array count];j++)
{
if([[array objectAtIndex:i] floatValue] < [[array objectAtIndex:j] floatValue])
{
[array exchangeObjectAtIndex:i withObjectAtIndex:j];
}
}
}
return array;
}
关于iphone - 我可以对 nsuserdefaults 中存储的分数进行排序吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12258044/