1

我有模型类,其中包含NSString's-studentName和. 我想根据. 我所做的是studentRankstudentImageNSMutableArraystudentRanks

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

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

4

3 回答 3

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 将包含具有相同等级的分组数组。我没有测试运行代码,所以你可能需要修复一些不合适的东西。希望能帮助到你

于 2011-09-14T09:04:29.967 回答
1

如果要按排名顺序显示,则应设置ascendingYES

NSSortDescriptor * sortByRank = [[NSSortDescriptor alloc] initWithKey:@"studentRank" ascending:YES];
于 2011-09-14T08:24:55.053 回答
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];
}
于 2011-09-14T10:00:56.493 回答