2012-08-09 67 views
0
arrAll = [[NSMutableArray alloc] init ]; 

for (int i=0; i<3; i++) { 
    if (isCategory && i==0) { 
     secCount++; 
     //   NSLog(@"cate %@",arrCategory); 
     NSMutableArray *arrone = [[NSMutableArray alloc] init ]; 
     for (int j=0; j<arrCategory.count; j++) { 
      NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys: 
            [[arrCategory objectAtIndex:j] valueForKey:@"SuCategoriesString"],@"title", 
            [[arrCategory objectAtIndex:j] valueForKey:@"CategoryName"], @"subtitle", 
            [[arrCategory objectAtIndex:j] valueForKey:@"Favourite"] ,@"Favourite", 
            [[arrCategory objectAtIndex:j] valueForKey:@"Views"], @"Views", 
            [[arrCategory objectAtIndex:j] valueForKey:@"CommentCount"], @"CommentCount", 
            [[arrCategory objectAtIndex:j] valueForKey:@"Likes"],@"Likes", 
            [[arrCategory objectAtIndex:j] valueForKey:@"CatId"],@"CatId", 
            nil]; 
      [arrone addObject:dict]; 
      [dict release]; 
     } 
     NSDictionary *Dictmain = [[NSDictionary alloc] initWithObjectsAndKeys: 
            @"CATEGORIES",@"titHeader", 
            arrone, @"arrayFill", nil]; 
     [arrAll addObject:Dictmain]; 
     [arrone release]; 
     [Dictmain release]; 
    } 
    if (isTopic && i==1) 
    { 
     secCount++; 
     NSMutableArray *arrone = [[NSMutableArray alloc] init ]; 
     for (int j=0; j<arrTopic.count; j++) { 
      NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys: 
            [[arrTopic objectAtIndex:j] valueForKey:@"TopicName"],@"title", 
            [[arrTopic objectAtIndex:j] valueForKey:@"CategoryName"], @"subtitle", 
            [[arrTopic objectAtIndex:j] valueForKey:@"Favourite"] ,@"Favourite", 
            [[arrTopic objectAtIndex:j] valueForKey:@"Views"], @"Views", 
            [[arrTopic objectAtIndex:j] valueForKey:@"CommentCount"], @"CommentCount", 
            [[arrTopic objectAtIndex:j] valueForKey:@"Likes"],@"Likes", 
            [[arrTopic objectAtIndex:j] valueForKey:@"TopicId"],@"TopicId", 
            [[arrTopic objectAtIndex:j] valueForKey:@"SubCategoryName"],@"SubCategoryName", 
            nil]; 
      [arrone addObject:dict]; 
      [dict release]; 
     } 
     NSDictionary *Dictmain = [[NSDictionary alloc] initWithObjectsAndKeys: 
            @"TOPICS",@"titHeader", 
            arrone,@"arrayFill", nil]; 
     [arrAll addObject:Dictmain]; 
     [arrone release]; 
     [Dictmain release]; 
    } 
    if (isTip && i==2) 
    { 
     secCount++; 
     NSMutableArray *arrone = [[NSMutableArray alloc] init ]; 
     for (int j=0; j<arrTips.count; j++) { 
      NSDictionary *dict = [[NSDictionary alloc] initWithObjectsAndKeys: 
            [[arrTips objectAtIndex:j] valueForKey:@"TipName"],@"title", 
            [[arrTips objectAtIndex:j] valueForKey:@"TopicNameString"], @"subtitle", 
            [[arrTips objectAtIndex:j] valueForKey:@"Favourite"] ,@"Favourite", 
            [[arrTips objectAtIndex:j] valueForKey:@"Views"], @"Views", 
            [[arrTips objectAtIndex:j] valueForKey:@"CommentCount"], @"CommentCount", 
            [[arrTips objectAtIndex:j] valueForKey:@"Likes"],@"Likes", 
            [[arrTips objectAtIndex:j] valueForKey:@"TipId"],@"TipId", 
            nil]; 
      [arrone addObject:dict]; 
      [dict release]; 
     } 
     NSDictionary *Dictmain = [[NSDictionary alloc] initWithObjectsAndKeys:@"TIPS",@"titHeader", 
            arrone, @"arrayFill", nil]; 
     [arrAll addObject:Dictmain]; 
     [arrone release]; 
     [Dictmain release]; 
    } 
} 
[arrAll retain]; 
NSLog(@"Array before sorting:%@",arrAll); 

我希望arrA​​ll可以基於Views,CommentCount,Likes,Favorites和我想要的結果相同,任何機構都可以幫助我解決這個問題。 取悅用於格式化排序字典陣列

回答

0

抱歉下面是可能的數組排序功能從NSArray Class Reference選擇:

– sortedArrayHint 
– sortedArrayUsingFunction:context: 
– sortedArrayUsingFunction:context:hint: 
– sortedArrayUsingDescriptors: 
– sortedArrayUsingSelector: 
– sortedArrayUsingComparator: 
– sortedArrayWithOptions:usingComparator: 
+0

感謝這幫了我 – Anand 2012-08-17 11:16:37

0

這僅僅是一個數組排序方法。你必須使用NSSortDescriptor

NSSortDescriptor *lastNameDescriptor = [[NSSortDescriptor alloc] initWithKey:@"CATALOG_NO" ascending:YES]; 
[nameArray sortUsingDescriptors:[NSArray arrayWithObject:lastNameDescriptor]]; 

其中initWithKey,你必須使用你有排序和名稱數組是你的陣列,它包含您的陣列

+0

這是做aphanumaric的基礎上排序,而不是關鍵的類型 – Anand 2012-08-17 11:16:10

+0

感謝您的努力,並對很晚的回覆感到抱歉 – Anand 2012-08-17 11:16:57

+0

它排序字母數字或字母你的理由您需要傳遞您的數組對象密鑰 – Hiren 2012-08-17 11:20:51

0
NSSortDescriptor *sortDesc = [[NSSortDescriptor alloc] initWithKey:sortString ascending:NO comparator:^NSComparisonResult(id firstObject, id secondObject) 
      { 
       return [((NSString *)firstObject) compare:((NSString *)secondObject) options:NSNumericSearch]; 
      }]; 

      arrContent = [arrContentOriginal sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDesc]]; 

發現這個解決方案

其值數組你的關鍵價值