数组嵌套字典
NSArray<__kindof NSDictionary *> *array;
根据array内部的Dictionary某个Key排序
//字典数组按KEY排序
+ (NSArray *)sortArray:(NSArray *)array withKey:(NSString *)key {
NSSortDescriptor *sortDesc = [NSSortDescriptor sortDescriptorWithKey:key ascending:YES];
NSArray *sortDescArr = [NSArray arrayWithObject:sortDesc];
NSArray *sortedArr = [array sortedArrayUsingDescriptors:sortDescArr];
return sortedArr;
}
Array怎么排序
Array是根据sortDescriptors进行排序,其本身是一个NSSortDescriptor的Array.所以只需要根据key来创建NSSortDescriptor,并放入一个数组就可以了.
@interface NSArray<ObjectType> (NSSortDescriptorSorting)
- (NSArray<ObjectType> *)sortedArrayUsingDescriptors:(NSArray<NSSortDescriptor *> *)sortDescriptors; // returns a new array by sorting the objects of the receiver
@end
@interface NSMutableArray<ObjectType> (NSSortDescriptorSorting)
- (void)sortUsingDescriptors:(NSArray<NSSortDescriptor *> *)sortDescriptors; // sorts the array itself
@end