0
   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;
     }
4

2 回答 2

0

我只是发布你必须考虑的事情。

请参阅NSArray文档sortedArrayUsingFunction:context:中的方法。

也有类似的方法NSMutableArray

请检查那些。

于 2012-09-06T06:45:15.527 回答
0

查看 NSMutableArray 上的各种 'sortUsing...' 方法 - 您不必实现自己的排序。

例如这里是一些关于如何使用'sortUsingComparator'的文档

sortUsingComparator:使用给定 NSComparator 块指定的比较方法对数组进行排序。

- (void)sortUsingComparator:(NSComparator)cmptr

您在比较操作中使用 NSComparator 块,例如 NSArray 的 sortedArrayUsingComparator:,例如:

NSArray *sortedArray = [array sortedArrayUsingComparator: ^(id obj1, id obj2) {

    if ([obj1 integerValue] > [obj2 integerValue]) {
        return (NSComparisonResult)NSOrderedDescending;
    }

    if ([obj1 integerValue] < [obj2 integerValue]) {
        return (NSComparisonResult)NSOrderedAscending;
    }
    return (NSComparisonResult)NSOrderedSame;
}];
于 2012-09-06T06:45:20.130 回答