对NSMutableArray中的NSDictionary对象自定义排序
引用:http://www.i-alive.com/post/149/
写一个 category 给类NSDictonary添加一个新的compare方法:
1 #import <Foundation/Foundation.h> 2 3 @interface NSDictionary (NSDictionaryHelper) 4 5 //对NSMutableArray中的NSDictionary对象自定义排序 6 - (NSComparisonResult)compareScore:(NSDictionary*)otherObject; 7 8 @end
1 #import "NSDictionary+NSDictionaryHelper.h" 2 3 @implementation NSDictionary (NSDictionaryHelper) 4 5 - (NSComparisonResult)compareScore:(NSDictionary*)otherObject { 6 //对第一个对象排序,也可以是其他对象 7 return [[[otherObject allValues] objectAtIndex:0] 8 compare:[[self allValues] objectAtIndex:0]]; 9 } 10 11 @end
在代码中调用这个方法
1 #import "NSDictionary+NSDictionaryHelper.h" 2 .......... 3 //进行排序 4 studentArray = [NSMutableArray arrayWithArray: 5 [studentArray sortedArrayUsingSelector:@selector(compareScore:)]];