热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

如何按整数对数组进行排序-howtosortanarraybyinteger

iamtryingtosortmyscoreboardbutthescoreboardissortedbynamebutiwantsortitbyscore

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 个解决方案

#1


3  

Here you are adding the string (containing name and score) to the array. Instead, create a model class with the name (NSString*) and score (NSNumber*) members and add them to the array.

在这里,您将字符串(包含名称和分数)添加到数组中。而是创建一个名为(NSString *)和score(NSNumber *)成员的模型类,并将它们添加到数组中。

For sorting,

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;
}];

推荐阅读
author-avatar
dmcm0008
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有