作者:dmcm0008 | 来源:互联网 | 2023-10-10 20:02
i am trying to sort my score board but the score board is sorted by name but i want sort it by score i.e (high to low) score plese help to solve this.i stored the playername and score details in one label.
please help me thanks in advance
我正在尝试对我的记分板进行排序,但记分板按名称排序,但我希望按分数排序,即从高到低分数,这有助于解决这个问题。我将玩家名称和得分细节存储在一个标签中。请提前帮助我
asd 45 asd 66 rrr 55 tes 42
asd 45 asd 66 rrr 55 tes 42
i want to show like
我希望表现得像
asd 66 rrr 55 asd 45 tes 42
asd 66 rrr 55 asd 45 tes 42
-(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]];
int intScore = iTap;
NSLog(@"iTap data is:--> %d",intScore);
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]];
NSSortDescriptor *sorter = [[NSSortDescriptor alloc] initWithKey:@"self" ascending:YES];
NSArray *sorters = [[NSArray alloc] initWithObjects:sorter, nil];
[sorter release];
NSArray *sortedArray = [array sortedArrayUsingDescriptors:sorters];
[sorters release];
NSUserDefaults *dfltsData = [NSUserDefaults standardUserDefaults];
[dfltsData setObject:sortedArray forKey:@"ScoreName"];
// [dfltsData setObject:array forKey:@"ScoreCard"];
[dfltsData synchronize];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert"
message:@"Score is saved."
delegate:self
cancelButtonTitle:@"Ok"
otherButtonTitles:nil, nil];
[alert show];
[alert release];
}
1 个解决方案