2011-09-14 156 views
1

我有一个包含NSString's- studentNamestudentRankstudentImage模型类。我根据studentRanks想排序NSMutableArray。我做了什么是如何对NSMutableArray元素进行排序?

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

但是这不能正确显示结果。请帮我解决这个问题。提前致谢。

+0

请问最后要存储在localObjects中的值吗? –

+0

这种情况下显示的结果是什么? – Joze

+0

studentName,studentRank和studentImage在视图中。 – Lion

回答

1

如果要按等级顺序显示,则应将ascending设置为YES

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

好的。首先感谢,但我怎么能将他们排在同一行列。 – Lion

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

+0

Ok-Rahul。首先我会检查这个,如果工作,我会接受你的答案。谢谢 – Lion

+0

没有问题狮子..同时,如果你遇到任何好的方法,节省时间与我们所有人分享..快乐编码 –

+0

***由于未捕获的异常“NSInvalidArgumentException”终止应用程序,原因:' - [__ NSArrayM studentRank] :无法识别的选择器发送到实例0x69ad7d0'。在此行中的NSLog(@ “arrGroupedStudents名称 - %@排名 - %@”,object.studentName,object.studentRank); – Lion

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]; 
} 
相关问题