现在的位置: 首页 > 综合 > 正文

IOS 中给数据排序的两种方法—持续更新中—

2013年12月05日 ⁄ 综合 ⁄ 共 2726字 ⁄ 字号 评论关闭

// 方法一  比 方法二 快 10倍以上,分别为大概0.000010 、0.000110左右

// 但是  方法二 更节省代码, 所以我还是采用方法 二

//    float themaxBloodSugar; // 最大值
//    float theminBloodSugar; // 最小值
    float thesumBloodSugar; // 平均值
//    float themaxWeight;     // 最大值 weight
//    float theminWeight;     // 最小值 weight
    float thesunWeight;     // 平均值 weight
    tb_History *maxHistory = (tb_History *)[historys objectAtIndex:0];                 
    tb_History *minHistory = (tb_History *)[historys objectAtIndex:0];
    tb_History *maxWeightHistory = (tb_History *)[historys objectAtIndex:0];                 
    tb_History *minWeightHistory = (tb_History *)[historys objectAtIndex:0];
    tb_History *lastHistory = (tb_History *)[historys objectAtIndex:0];
    
//    tb_History *tmpHistory = (tb_History *)[historys objectAtIndex:0];
//    themaxBloodSugar = [tmpHistory.BloodSugar floatValue];
//    theminBloodSugar = [tmpHistory.BloodSugar floatValue];
//    thesumBloodSugar = [tmpHistory.BloodSugar floatValue];
//    themaxWeight = [tmpHistory.Weight floatValue];
//    theminWeight = [tmpHistory.Weight floatValue];
//    thesunWeight = [tmpHistory.Weight floatValue];
//    
//    //方法一:
//    
//    NSDate *date1 = [NSDate date];
//    
//    //最大值、最小值、平均值
//    //weight 最大值、最小值、平均值
//    for (int i = 1; i < recycleCount; ++i) {
//        tb_History *thetmpHistory = (tb_History *)[historys objectAtIndex:i];
//        
//        //bloodSugar
//        if (thetmpHistory.BloodSugar.floatValue - themaxBloodSugar > 0.000001)
//        {
//            maxHistory = thetmpHistory;
//            themaxBloodSugar = thetmpHistory.BloodSugar.floatValue;
//        }
//        if (theminBloodSugar - thetmpHistory.BloodSugar.floatValue >0.000001) {
//            minHistory = thetmpHistory;
//            theminBloodSugar = thetmpHistory.BloodSugar.floatValue;
//        }
//        thesumBloodSugar += thetmpHistory.BloodSugar.floatValue;
//        
//        //weight
//        if (thetmpHistory.Weight.floatValue - themaxWeight > 0.000001)
//        {
//            maxWeightHistory = thetmpHistory;
//            themaxWeight = thetmpHistory.Weight.floatValue;
//        }
//        if (theminWeight - thetmpHistory.Weight.floatValue >0.000001) {
//            minWeightHistory = thetmpHistory;
//            theminWeight = thetmpHistory.Weight.floatValue;
//        }
//        thesunWeight += thetmpHistory.Weight.floatValue;
//    }
//    
//    NSDate *date2 = [NSDate date];
//    NSLog(@"%f",[date2 timeIntervalSinceDate:date1]);
    
    // 方法二:
    
    NSDate *date1 = [NSDate date];
    
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]initWithKey:@"BloodSugar" ascending:NO];
    [historys sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
    
    maxHistory = [historys objectAtIndex:0];
    minHistory = [historys objectAtIndex:historys.count - 1];
    for (tb_History *oneHistory in historys) {
        thesumBloodSugar += oneHistory.BloodSugar.floatValue;
        thesunWeight += oneHistory.Weight.floatValue;
    }
    [sortDescriptor release];
    
    NSSortDescriptor *sortWeightDescriptor = [[NSSortDescriptor alloc]initWithKey:@"Weight" ascending:NO];
    [historys sortUsingDescriptors:[NSArray arrayWithObject:sortWeightDescriptor]];
    
    maxWeightHistory = [historys objectAtIndex:0];
    minWeightHistory = [historys objectAtIndex:historys.count - 1];
    
    [sortWeightDescriptor release];
    
    NSDate *date2 = [NSDate date];
    NSLog(@"%f",[date2 timeIntervalSinceDate:date1]);

抱歉!评论已关闭.