我有一个包含多个词典的数组。 见下面的例子:
示例:
Main Array:
(
{
Title: "A";
Value: 1;
Priority:
(
4 //Problem: contains array as value which need to be sorted
);
},
{
Title: "B";
Value: 2;
Priority:
(
3 //Problem: contains array as value which need to be sorted
);
},
{
Title: "C";
Value: 3;
},
{
Title: "D";
Value: 4;
Priority:
(
);
},
{
Title: "E";
Value: 5;
Priority: null
},
)
我需要根据dictioanry中的“Priority”对主数组进行排序。 优先级值是一个数组。
如何实现这种排序?
帮我解决这个问题......
提前致谢
答案 0 :(得分:0)
您可以使用NSSortDescriptor
NSSortDescriptor *desc = [[NSSortDescriptor alloc] initWithKey:@"Title" ascending:YES];
[arr sortUsingDescriptors:[NSArray arrayWithObject:desc]];
或其他选项可以是sortedArrayUsingComparator
[arr sortedArrayUsingComparator:^(NSDictionary *item1, NSDictionary *item2) {
NSString *first = [item1 objectForKey:@"Title"];
NSString *second = [item2 objectForKey:@"Title"];
return [first compare:second options:NSLiteralSearch];
}];
答案 1 :(得分:0)
试试这个。如果您需要反向,那么只需更改if-else条件的顺序
[mainArray sortedArrayUsingComparator:^(NSDictionary *dic1, NSDictionary *dic2) {
int priority1 = [[[dic1 objectForKey:@"Priority"] objectAtIndex:0] intValue];
int priority2 = [[[dic2 objectForKey:@"Priority"] objectAtIndex:0] intValue];
if(priority1 < priority2) return NSOrderedAscending;
else if(priority1 > priority2) return NSOrderedDescending;
else return NSOrderedSame;
}];
答案 2 :(得分:0)
NSArray *mainArray = @[
@{
@"Title":@"A",
@"value":@"1",
@"priority":@[[NSNumber numberWithInt:4]]
},
@{
@"Title":@"F",
@"value":@"1",
@"priority":@[[NSNumber numberWithInt:1]]
},
@{
@"Title":@"E",
@"value":@"1",
@"priority":@[]
},
@{
@"Title":@"C",
@"value":@"1",
@"priority":@[[NSNumber numberWithInt:3]]
},
@{
@"Title":@"D",
@"value":@"1",
@"priority":@[[NSNumber numberWithInt:5]]
},
@{
@"Title":@"B",
@"value":@"1",
@"priority":@[[NSNumber numberWithInt:2]]
},
@{
@"Title":@"E",
@"value":@"1",
@"priority":@[]
},
@{
@"Title":@"G",
@"value":@"1",
@"priority":[NSNull null]
}
];
NSArray *sortedArray = [mainArray sortedArrayUsingComparator:^(NSDictionary *dic1, NSDictionary *dic2) {
int priority1 = 0,priority2 = 0;
if([[dic1 objectForKey:@"priority"] isKindOfClass:[NSArray class]]){
NSArray *arr = (NSArray *)[dic1 objectForKey:@"priority"];
if(arr.count != 0)
priority1 = [[arr objectAtIndex:0] intValue];
}
if([[dic2 objectForKey:@"priority"] isKindOfClass:[NSArray class]]){
NSArray *arr = (NSArray *)[dic2 objectForKey:@"priority"];
if(arr.count != 0)
priority2 = [[arr objectAtIndex:0] intValue];
}
if(priority1 > priority2) return NSOrderedAscending;
else if(priority1 < priority2) return NSOrderedDescending;
else return NSOrderedSame;
}];
> The OutPut is in my LOG:
>
> 2014-11-10 12:09:20.925 Sort Array[1624:591502] sorted array: (
> {
> Title = D;
> priority = (
> 5
> );
> value = 1;
> },
> {
> Title = A;
> priority = (
> 4
> );
> value = 1;
> },
> {
> Title = C;
> priority = (
> 3
> );
> value = 1;
> },
> {
> Title = B;
> priority = (
> 2
> );
> value = 1;
> },
> {
> Title = F;
> priority = (
> 1
> );
> value = 1;
> },
> {
> Title = E;
> priority = (
> );
> value = 1;
> },
> {
> Title = E;
> priority = (
> );
> value = 1;
> },
> {
> Title = G;
> priority = "<null>";
> value = 1;
> } )