iOS开发之数组排序(数组中包括字典)
2013-09-24 00:03
477 查看
iOS开发之数组排序(数组中包括字典)
1。普通数组排序: NSMutableArray *arr = [NSMutableArray arrayWithObjects:@"0",@"8",@"6",@"1",nil];NSArray *sortedArray = [arr sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2){
if ([obj1 intValue] > [obj2 intValue]){
return NSOrderedDescending;
}
if ([obj1 intValue] < [obj2 intValue]){
return NSOrderedAscending;
}
return NSOrderedSame;
}];
NSLog(@"排序后的数组:%@",sortedArray);
2.数组中有字典的排序:
NSMutableArray *myMutableArr = [[[NSMutableArray alloc] init] autorelease];
NSDictionary *dicOne = [NSDictionary dictionaryWithObjectsAndKeys:@"1",@"price",@"2",@"number", nil];
NSDictionary *dicTWo = [NSDictionary dictionaryWithObjectsAndKeys:@"6",@"price",@"5",@"number", nil];
NSDictionary *dicThree = [NSDictionary dictionaryWithObjectsAndKeys:@"3",@"price",@"1",@"number", nil];
NSDictionary *dicFour = [NSDictionary dictionaryWithObjectsAndKeys:@"4",@"price",@"3",@"number", nil];
[myMutableArr addObject:dicOne];
[myMutableArr addObject:dicTWo];
[myMutableArr addObject:dicThree];
[myMutableArr addObject:dicFour];
NSArray *sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"price" ascending:YES]];
[myMutableArr sortUsingDescriptors:sortDescriptors];
NSLog(@"排序后的数组%@",myMutableArr);
经验证,上面的方式相当方便,尤其是数组中有字典的情况。
相关文章推荐
- ios开发数组排序(数组中包括字典)
- ios开发数组排序(数组中包括字典)
- ios开发数组排序(数组中包括字典)
- ios开发数组排序(数组中包括字典)
- iOS开发——根据数组中的字典中的某一元素排序
- iphone开发数组排序(数组中包括字典)
- ios开发 数组排序——根据数组内字典的指定属性排序
- iphone开发数组排序(数组中包括字典)
- iOS开发笔记之元素为字典的数组,按元素的value排序
- iOS开发之数组排序
- iOS开发数组去重方法,排序去重和包含元素
- iOS 数组中的string 按照字典排序
- iOS开发数组去重方法,排序去重和包含元素
- iOS中对字典进行排序 ios开发教程
- iOS开发之数组、字典、集合
- iOS开发之数组去重与排序
- iOS开发数组去重方法,排序去重和包含元素
- iOS开发 -- 数组去重+排序的方法
- iOS开发-- 使用NSNumber将int、float、long等数据类型加入到数组或字典中
- ios字典存到数组当中进行年龄降序排序,然后删除指定的成员信息