如何按值对plist排序 [英] How to sort plist by its value

查看:100
本文介绍了如何按值对plist排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有字典列表(item0,item1,item2)的Plist.并且我在Graph中填充了这个plist ..它很好用.并且在Plist key (date)-> Value(i store by using NSDate)中.现在我需要在这样的列表中对Plist进行排序一种方式:- graph应该只显示一个星期. 假设第一个值是26-Dec-12,而最多只能显示1-Jan-13(1周)值plist应该显示

I have Plist with list with List of Dictionaries(item0,item1,item2).and I populate this plist in the Graph..its works fine.and in Plist key (date)-> Value(i store by using NSDate) .Now I need to sort the Plist in such a way that:- graph should display for only one week. say if first value is 26-Dec-12 than only upto 1-Jan-13(1 week) values plist should display

代码:

- (NSArray *)readFromPlist
{
// get paths from root direcory
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                             NSUserDomainMask, YES); 
NSString *documentsDirectory = [documentPaths objectAtIndex:0];
NSString *documentPlistPath = [documentsDirectory stringByAppendingPathComponent:@"calori.plist"];

NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:documentPlistPath];

valueArray = [dict objectForKey:@"title"];

  return valueArray;
}

- (void)drawRect:(CGRect)rect {
// Drawing code


    CGContextRef _context = UIGraphicsGetCurrentContext();
ECGraph *graph = [[ECGraph alloc] initWithFrame:CGRectMake(10,10, 480, 320) 
                                    withContext:_context isPortrait:NO];

NSMutableArray *Array=[NSMutableArray arrayWithArray:[self readFromPlist]];


NSMutableArray *items = [[NSMutableArray alloc] init];

for (id object in [Array reverseObjectEnumerator]){

    if ([object isKindOfClass:[NSDictionary class]])
    {
        NSDictionary *objDict = (NSDictionary *)object;


           tempItemi  =[[ECGraphItem alloc]init];

        NSString *str=[objDict objectForKey:@"title"];
        NSLog(@"str value%@",str);
        float f=[str floatValue];


        NSString*str1=[objDict objectForKey:@"date"];

        NSLog(@" str values2-- %@",str1);

        tempItemi.isPercentage=YES;
        tempItemi.yValue=f;

        tempItemi.name=str1;



         [items addObject: tempItemi];



    }
}

[graph drawHistogramWithItems:items lineWidth:2 color:[UIColor blackColor]];
}

推荐答案

您的要求是在7天内过滤日期,

As your requirement is to filter date within 7 days,

我给你一个逻辑,尝试这种方式:

I am giving you a logic, try this way:

- (NSArray *)readFromPlistForOneWeek {

    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES); 
    NSString *documentsDirectory = [documentPaths objectAtIndex:0];
    NSString *documentPlistPath = [documentsDirectory stringByAppendingPathComponent:@"calori.plist"];


    NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:documentPlistPath];

    //loop through each of the item
    //and check if <8 then add that keyValue to array
    NSMutableArray *tempValueArray=[NSMutableArray new];
    for (NSDictionary *subDict in [dict objectForKey:@"title"]) {
       // NSLog(@"=> %@",subDict);

        NSString *plistDateString=[subDict objectForKey:@"date"];
        NSDate *currentDate = [NSDate date];

        NSDateFormatter *dateFormatter=[NSDateFormatter new];
        [dateFormatter setDateFormat:@"dd-MMM-yy"];

        NSDate *plistDate=[dateFormatter dateFromString:plistDateString];

        NSString *currentDateString=[dateFormatter stringFromDate:currentDate];

        NSTimeInterval secondsBetween = [plistDate timeIntervalSinceDate:currentDate];
        NSInteger dateDiff = secondsBetween / 86400;

        if( dateDiff<8 ){ //within 0-7 days
            [tempValueArray addObject:subDict];
        }
    }

    NSLog(@"valuArray : %@",tempValueArray);

    return tempValueArray;
}

这篇关于如何按值对plist排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆