对字典的 NSArray 的 NSArray 进行排序
Sort NSArray of NSArray of dictionaries
我有一个 parentArray,其索引处有子数组。每个子数组都有一个包含多个键值对的字典。一个键有一个子字典作为它的值。基于这个子字典的值之一,我想对 parentArray 进行排序。
ParentArray [
ChildArray [{
parentKey: {
childKey: <the value to sort parentArray>
}
}],
ChildArray [{
parentKey: {
childKey: <the value to sort parentArray>
}
}]
]
childKey 的值是我要根据其对 ParentArray 进行排序的值。
arrParent = [arrParent sortedArrayUsingComparator:^NSComparisonResult(id arrChild1, id arrChild2) {
NSDictionary *dict1 = [(NSArray *)arrChild1 objectAtIndex:0];
NSDictionary *dict2 = [(NSArray *)arrChild2 objectAtIndex:0];
int user1 = [[dict1 objectForKey:@"id"] intValue];
int user2 = [[dict2 objectForKey:@"id"] intValue];
if (user1 > user2)
{
return (NSComparisonResult)NSOrderedAscending;
}
else if (user1 < user2)
{
return (NSComparisonResult)NSOrderedDescending;
}
return (NSComparisonResult)NSOrderedSame;
}];
得到帮助here
我有一个 parentArray,其索引处有子数组。每个子数组都有一个包含多个键值对的字典。一个键有一个子字典作为它的值。基于这个子字典的值之一,我想对 parentArray 进行排序。
ParentArray [
ChildArray [{
parentKey: {
childKey: <the value to sort parentArray>
}
}],
ChildArray [{
parentKey: {
childKey: <the value to sort parentArray>
}
}]
]
childKey 的值是我要根据其对 ParentArray 进行排序的值。
arrParent = [arrParent sortedArrayUsingComparator:^NSComparisonResult(id arrChild1, id arrChild2) {
NSDictionary *dict1 = [(NSArray *)arrChild1 objectAtIndex:0];
NSDictionary *dict2 = [(NSArray *)arrChild2 objectAtIndex:0];
int user1 = [[dict1 objectForKey:@"id"] intValue];
int user2 = [[dict2 objectForKey:@"id"] intValue];
if (user1 > user2)
{
return (NSComparisonResult)NSOrderedAscending;
}
else if (user1 < user2)
{
return (NSComparisonResult)NSOrderedDescending;
}
return (NSComparisonResult)NSOrderedSame;
}];
得到帮助here