// 升序
// 数组排序
// 定义一个数字数组
NSArray *array = @[@(3),@(4),@(2),@(1)];
// 对数组进行排序
NSArray *result = [array sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
XPFLog(@"%@~%@",obj1,obj2); // 3~4 2~1 3~1 3~2
return [obj1 compare:obj2]; // 升序
}];
XPFLog(@"result=%@",result);
// 降序
// 数组排序
// 定义一个数字数组
NSArray *array2 = @[@(13),@(14),@(12),@(11)];
// 对数组进行排序
NSArray *result2 = [array2 sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
XPFLog(@"%@~%@", obj1, obj2); // 3~4 2~1 3~1 3~2
return [obj2 compare:obj1]; // 降序
}];
XPFLog(@"result= %@",result2);
方法一:
NSArray *sortArray = [arrayM sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
SDHomeNewTaskModel *model1 = obj1;
SDHomeNewTaskModel *model2 = obj2;
if ([model1.distance floatValue] > [model2.distance floatValue]) {
return NSOrderedDescending;//降序
}else if ([model1.distance floatValue] > [model2.distance floatValue]){
return NSOrderedAscending;//升序
}else {
return NSOrderedSame;//相等
}
}];
for (SDHomeNewTaskModel *model in sortArray) {
NSLog(@"distance3:------> %@", model.distance);
}
方法二:
NSMutableArray *arr = [NSMutableArray array];
for (int i =0; i < arrayM.count; i ++) {
SDHomeNewTaskModel *model = arrayM[i];
[arr addObject:model];
NSLog(@"distance2:------> %@", model.distance);
}
//这里类似KVO的读取属性的方法,直接从字符串读取对象属性,注意不要写错ascending:指定一个集合是否按照升序(YES)还是降序(NO)
NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"distance" ascending:YES];
//这个数组保存的是排序好的对象
NSArray *arr = arrayM;
NSArray *tempArray = [arr sortedArrayUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
// 输出排序结果
for (SDHomeNewTaskModel *model in tempArray) {
NSLog(@"distance3:------> %@", model.distance);
}
方法三:
-(NSString*)stringWithDict:(NSDictionary*)dict {
NSArray*keys = [dict allKeys];
NSArray*sortedArray = [keys sortedArrayUsingComparator:^NSComparisonResult(id obj1,id obj2) {
return [obj1 compare:obj2 options:NSNumericSearch];
}];
NSString *str = @"";
for(NSString*categoryId in sortedArray) {
id value = [dict objectForKey:categoryId];
if([value isKindOfClass:[NSDictionary class]]) {
value = [self stringWithDict:value];
}
NSLog(@"[dict objectForKey:categoryId] === %@",[dict objectForKey:categoryId]);
if([str length] !=0) {
str = [str stringByAppendingString:@";"];
}
str = [str stringByAppendingFormat:@"%@:%@",categoryId,value];
}
return str;
}
方法四:
-(NSString *)getNeedSignStrFrom:(id)obj{
NSDictionary *dict = obj;
NSArray *arrPrimary = [dict.allKeys mutableCopy];
NSArray *arrKey = [arrPrimary sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2){
NSComparisonResult result = [obj1 compare:obj2];
return result==NSOrderedDescending;//NSOrderedAscending 倒序
}];
NSString*str =@"";
for (NSString *s in arrKey) {
id value = dict[s];
if([value isKindOfClass:[NSDictionary class]]) {
value = [self getNeedSignStrFrom:value];
}
if([str length] !=0) {
str = [str stringByAppendingString:@","];
}
str = [str stringByAppendingFormat:@"%@:%@",s,value];
}
NSLog(@"str:%@",str);
return str;
}
参考1:进哥哥爱你
参考2:公羽寒
网友评论