标签:
// 数组用系统方法compare做字母的简单排序
NSArray *oldArray = @[@"bac",@"bzd",@"azc",@"azz"];
NSArray *newArray = [oldArray sortedArrayUsingSelector:@selector(compare:)];
NSLog(@"new array = %@",newArray);
// 因为NSDictionary排序
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setObject:@"1" forKey:@"key1"];
[dict setObject:@"2" forKey:@"key2"];
[dict setObject:@"3" forKey:@"key3"];
[dict setObject:@"4" forKey:@"key4"];
for (NSString *str in [dict allKeys]) {
NSLog(@"key == %@",str);
}
NSArray *keys = [dict allKeys];
NSArray *sortedArray = [keys sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2){
return [obj1 compare:obj2 options:NSNumericSearch];
}];
for (NSString *categoryId in sortedArray) {
NSLog(@"[dict objectForKey:categoryId] === %@",[dict objectForKey:categoryId]);
}
标签:
原文地址:http://www.cnblogs.com/NSong/p/5372002.html