因要对NSDictionary数组排序,且取前n条,整理一下代码
NSMutableArray *myMutableArr = [[NSMutableArray alloc] init] ;
NSDictionary *dicOne = @{@"number":@"1",@"pid":@"2"};
NSDictionary *dicTWo = @{@"number":@"6",@"pid":@"5"};
NSDictionary *dicThree =@{@"number":@"3",@"pid":@"1"};
NSDictionary *dicFour = @{@"number":@"4",@"pid":@"3"};
[myMutableArr addObjectsFromArray:@[dicOne,dicTWo,dicThree,dicFour]];
对number字段排序并取出前2条数据测试
NSArray *sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"number" ascending:NO]];
[myMutableArr sortUsingDescriptors:sortDescriptors];
//排序后取数组前2条
NSLog(@"排序后的数组%@",[myMutableArr subarrayWithRange:NSMakeRange(0,2)]);
网友评论