name score
----- -----
Abby 34
b 40
c 70
d 20
我的代码是
-(void)btnSaveScore
{
if(!dictWinData)
dictWinData = [[NSMutableDictionary alloc] init];
scoreCardArray = [[[NSUserDefaults standardUserDefaults] valueForKey:@"ScoreCard"] mutableCopy];
if([scoreCardArray count] == 0)
{
scoreCardArray = [[NSMutableArray alloc] init];
scorearray = [[NSMutableArray alloc] init];
playerarray = [[NSMutableArray alloc] init];
}
else
{
scorearray = [scoreCardArray objectAtIndex:0];
playerarray = [scoreCardArray objectAtIndex:1];
[scoreCardArray removeAllObjects];
}
// NSMutableArray *strName = [NSString stringWithFormat:@"%@",strNameOFPlayer];
NSMutableArray *strScore = [NSString stringWithFormat:@"%@",[NSString stringWithFormat:@"%d",iTap]];
if([strNameOFPlayer length]==7)
{
[scorearray addObject:strScore];
[playerarray addObject:strNameOFPlayer];
}
else if ([strNameOFPlayer length] == 6)
{
[scorearray addObject:strScore];
[playerarray addObject:strNameOFPlayer];
}
else if ([strNameOFPlayer length] == 5)
{
[scorearray addObject:strScore];
[playerarray addObject:strNameOFPlayer];
}
else if ([strNameOFPlayer length] == 4)
{
[scorearray addObject:strScore];
[playerarray addObject:strNameOFPlayer];
}
else if ([strNameOFPlayer length] == 3)
{
[scorearray addObject:strScore];
[playerarray addObject:strNameOFPlayer];
}
else if ([strNameOFPlayer length] == 2)
{
[scorearray addObject:strScore];
[playerarray addObject:strNameOFPlayer];
}
else if ([strNameOFPlayer length] == 1)
{
[scorearray addObject:strScore];
[playerarray addObject:strNameOFPlayer];
}
[scoreCardArray addObject:scorearray];
[playerarray addObject:playerarray];
NSUserDefaults *dfltsData = [NSUserDefaults standardUserDefaults];
[dfltsData setObject:scoreCardArray forKey:@"ScoreCard"];
[dfltsData synchronize];
NSMutableArray *sortedArray = [self sortByfloatvalue:scoreCardArray array:playerarray];
arrScores = [sortedArray objectAtIndex:0];
arrPlayers = [sortedArray objectAtIndex:1];
NSLog(@" %@ \n %@",scoreCardArray,playerarray);
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert"
message:@"Score is saved."
delegate:self
cancelButtonTitle:@"Ok"
otherButtonTitles:nil, nil];
[alert show];
[alert release];
}
-(NSMutableArray *)sortByfloatvalue:(NSMutableArray *)scorearray1 array:(NSMutableArray *)playerarray1
{
NSMutableArray *sortedArr = [NSMutableArray array];
for(int k=0;k<[scorearray1 count];k++)
{
for(int l=k+1;l<[scorearray1 count];l++)
{
if([[scorearray1 objectAtIndex:k] floatValue] < [[scorearray1 objectAtIndex:l] floatValue])
{
[scorearray1 exchangeObjectAtIndex:k withObjectAtIndex:l];
[playerarray1 exchangeObjectAtIndex:k withObjectAtIndex:l];
}
}
}
[sortedArr addObject:scorearray1];
[sortedArr addObject:playerarray1];
return sortedArr;
}