繁体   English   中英

排序字典数组

[英]Sort an Array of Dictionaries

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,Favorites进行排序,并且我希望得到相同的结果,因为任何机构都可以通过它来帮助我。 请抱歉格式化

这是《 NSArray类参考》中可能的数组排序功能的选择:

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

这只是一个数组排序方法。 您必须使用NSSortDescriptor

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

在哪里initWithKey您必须在哪里使用键值必须对哪个值数组进行排序并命名Array是包含数组的数组

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

找到了这个解决方案

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM