zoukankan      html  css  js  c++  java
  • iOS

    方法一:
        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;
    }
  • 相关阅读:
    poj 3468 A Simple Problem with Integers 线段树区间更新
    poj 2096 概率dp
    JSP页面的基本结构 及声明变量
    怎样制作一个横版格斗过关游戏 Cocos2d-x 2.0.4
    块状元素与内联元素的差别
    ZOJ 3526 Weekend Party
    linux下javadoc生成文件出现中文乱码
    Centos6.0 通过devtoolset-2工具安装gcc 4.8
    fre7 offonline for firefox
    Aix Lamp openssh bash
  • 原文地址:https://www.cnblogs.com/gongyuhonglou/p/10649998.html
Copyright © 2011-2022 走看看