我想排序我的計分板,但計分板按名稱排序,但我想按分數排序(即從高到低)分數有助於解決this.i存儲的玩家名稱並在一個標籤中評分詳情。
請幫我 在此先感謝如何按整數排序數組
ASD 45 ASD 66 存款準備金率55個 TES 42
我想告訴喜歡
ASD 66 存款準備金率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];
}
你應該考慮使用字典來代替。 – Martol1ni