2011-09-14 49 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]; 
} 
相關問題