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);

私はarrAllをViews、CommentCount、Likes、Favouritesに基づいてソートしたいと思っています。また、これと同じ結果が欲しいのです。フォーマットしてごめんなさい

4

3 に答える 3

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

このソリューションを見つけました

于 2012-08-17T11:14:49.767 に答える
0

NSArray Class Referenceから可能な配列ソート関数の選択を次に示します。

– sortedArrayHint
– sortedArrayUsingFunction:context:
– sortedArrayUsingFunction:context:hint:
– sortedArrayUsingDescriptors:
– sortedArrayUsingSelector:
– sortedArrayUsingComparator:
– sortedArrayWithOptions:usingComparator:
于 2012-08-09T13:43:53.777 に答える
0

これは単なる配列のソート方法です。NSSortDescriptor を使用する必要があります

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

どこ initWithKey あなたがあなたのキー値を使用しなければならないところで、どの値の配列をソートして名前を付ける必要がありますか Array は、あなたの配列を含むあなたの配列です

于 2012-08-09T13:44:22.477 に答える