0

我正在尝试对我的记分牌进行排序,但记分牌是按名称排序的,但我想按分数排序,即(从高到低)分数请帮助解决这个问题。我将玩家姓名和分数详细信息存储在一个标签中。
请帮助我提前谢谢

asd 45 asd 66 rrr 55 测试 42

我想表现得像

asd 66 rrr 55 asd 45 测试 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];
}
4

1 回答 1

3

在这里,您将字符串(包含名称和分数)添加到数组中。相反,使用名称 (NSString*) 和分数 (NSNumber*) 成员创建模型类并将它们添加到数组中。

对于排序,

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

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

    if ([[obj1 score] integerValue] < [[obj2 score] integerValue]) {
        return NSOrderedAscending;
    }
    return NSOrderedSame;
}];
于 2012-09-06T07:04:43.623 回答