如何排序NSMutableArray元素?

时间:2011-09-14 08:16:14

标签: iphone ios nsmutablearray iphone-sdk-4.3

我的模型类包含NSString的 - studentNamestudentRankstudentImage。我想根据NSMutableArraystudentRanks进行排序。我所做的是

- (void)uploadFinished:(ASIHTTPRequest *)theRequest
{
    NSString *response = nil;
    response = [formDataRequest responseString];
    NSError *jsonError = nil;
    SBJsonParser *json = [[SBJsonParser new] autorelease];
    NSArray *arrResponse = (NSArray *)[json objectWithString:response error:&jsonError];
    if ([jsonError code]==0) {
        // get the array of "results" from the feed and cast to NSArray
        NSMutableArray *localObjects = [[[NSMutableArray alloc] init] autorelease];
        // loop over all the results objects and print their names
        int ndx;

        for (ndx = 0; ndx < arrResponse.count; ndx++) 
        {
            [localObjects addObject:(NSDictionary *)[arrResponse objectAtIndex:ndx]];
        }

        for (int x=0; x<[localObjects count]; x++) 
        {
            TopStudents *object = [[[TopStudents alloc] initWithjsonResultDictionary:[localObjects objectAtIndex:x]] autorelease];

            [localObjects replaceObjectAtIndex:x withObject:object];
        }

        topStudentsArray = [[NSMutableArray alloc] initWithArray:localObjects];

    }
}

如何根据学生的得分排序topStudentsArray,如果两个或更多学生的排名相同,我该如何分组。 我确实喜欢这个

TopStudents *object;
    NSSortDescriptor * sortByRank = [[[NSSortDescriptor alloc] initWithKey:@"studentRank" ascending:NO] autorelease];
    NSArray * descriptors = [NSArray arrayWithObject:sortByRank];
    NSArray * sorted = [topStudentsArray sortedArrayUsingDescriptors:descriptors];

但这不能正确显示结果。请帮我克服这个问题。提前谢谢。

3 个答案:

答案 0 :(得分:2)

做这样的事情可能会成功

Initially sort the arrGroupedStudents in the (ascending/descending) order of studentRank

//Create an array to hold groups
NSMutableArray* arrGroupedStudents = [[NSMutableArray alloc] initWithCapacity:[topStudentsArray count]];

for (int i = 0; i < [topStudentsArray count]; i++) 
{
    //Grab first student
    TopStudents* firstStudent = [topStudentsArray objectAtIndex:i];

    //Create an array and add first student in this array
    NSMutableArray* currentGroupArray = [[[NSMutableArray alloc] initWithCapacity:0] autorelease];
    [currentGroupArray addObject:firstStudent];

    //create a Flag and set to NO
    BOOL flag = NO;
    for (int j = i+1; j < [topStudentsArray count]; j++) 
    {
        //Grab next student
        TopStudents* nextStudent = [topStudentsArray objectAtIndex:j];  

        //Compare the ranks
        if ([firstStudent.studentRank intValue] == [nextStudent.studentRank intValue]) 
        {
            //if they match add this to same group
            [currentGroupArray addObject:nextStudent];
        }
        else {
            //we have got our group so stop next iterations
            [arrGroupedStudents addObject:currentGroupArray];
                           // We will assign j-1 to i
            i=j-1;
            flag = YES;
            break;
        }
    }
    //if entire array has students with same rank we need to add it to grouped array in the end
    if (!flag) {
        [arrGroupedStudents addObject:currentGroupArray];
    }
}

最后,您的arrGroupedStudents将包含具有相同排名的分组数组。我没有测试运行代码,所以你可能需要修复一些不合适的东西。希望它有所帮助

答案 1 :(得分:1)

如果您想按排名顺序显示,则应将ascending设为YES

NSSortDescriptor * sortByRank = [[NSSortDescriptor alloc] initWithKey:@"studentRank" ascending:YES];

答案 2 :(得分:0)

static int mySortFunc(NSDictionary *dico1, NSDictionary *dico2, void *context)
{
    NSString *studentName1 = [dico1 objectForKey:@"studentName"];
    NSString *studentName2 = [dico2 objectForKey:@"studentName"];
    return [studentName1 compare:studentName2];
}

- (IBAction)sortBtnTouched:(id)sender
{
    [topStudentsArray sortUsingFunction:mySortFunc context:NULL];
}