通常用于数据解析拿到JSON后,要根据某一个字典key对应的value将整个数组排序:
Demo如下:
NSArray *array = @[@{@"order":@4,@"appNo":@"test1"},
@{@"order":@7,@"appNo":@"test2"},
@{@"order":@1,@"appNo":@"test3"},
@{@"order":@8,@"appNo":@"test4"}];
NSMutableArray *arr = [array mutableCopy];
NSLog(@"arr...%@",array);
NSMutableArray *tmp =[NSMutableArray arrayWithCapacity:0];
for (int i = 0; i < [arr count];i++ ) {
for (int j = i+1; j < [arr count]; j++) {
NSDictionary *item = [arr objectAtIndex:i];
NSDictionary *item1 = [arr objectAtIndex:j];
int order = [[item objectForKey:@"order"]intValue];
int order1 = [[item1 objectForKey:@"order"]intValue];
if (order1 < order) {
//可变数组
[arr exchangeObjectAtIndex:j withObjectAtIndex:i];
}
tmp = arr;
}
}
NSLog(@"tmp....%@",tmp);
打印:
arr...(
{
appNo = test1;
order = 4;
},
{
appNo = test2;
order = 7;
},
{
appNo = test3;
order = 1;
},
{
appNo = test4;
order = 8;
}
)
tmp....(
{
appNo = test3;
order = 1;
},
{
appNo = test1;
order = 4;
},
{
appNo = test2;
order = 7;
},
{
appNo = test4;
order = 8;
}
)