sort NSMutableDictionary包含来自Json的带有日期字符串的对象

时间:2014-01-28 11:21:30

标签: ios iphone objective-c

我有一个带有日期键和名称键的可变词典日期键有三种方式的日期 - 过期日期,两个即将到来的日期和零(0000-00-00 YYYY-MM-DD)。 现在我想按顺序对这个NSDictionary数组进行排序。 即将到来的日期(升序)。 过期日期。 其他可能是nil我使用比较器和排序数组2格式一个数组没有空值和一个有值并附加这些数组但问题与它没有显示数组值按升序显示首先显示...

  NSMutableDictionary *JsonDict=[NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&err];
 // NSLog(@"jsondata :%@",JsonDict);

NSArray *arrayData=[[NSArray alloc]initWithArray:[JsonDict objectForKey:@"Product"]];

NSMutableArray* filtered =[[NSMutableArray alloc] init];
 NSMutableArray*unfiltered =[[NSMutableArray alloc] init];
   NSMutableArray*unfiltered1 =[[NSMutableArray alloc] init];

for (int i=0; i<[arrayData count]; i++)
{

    NSMutableDictionary *reviewsDict123=[arrayData objectAtIndex:i];

    if ([[reviewsDict123 objectForKey:@"date"]isEqualToString:@"0000-00-00"])
    {

        [filtered addObject:reviewsDict123];

    }

    else
    {

        [unfiltered addObject:reviewsDict123];


    }

}

未过滤阵列在2013年和2014年具有当前日期的价值混合,因此2013年价值已过期且2014年价值可用,因此我需要按2014年升值顺序排序,过期将在另一个阵列中。

NSSortDescriptor *sortDescriptors = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:NO];
sortedArray = [unfiltered sortedArrayUsingDescriptors:sortDescriptors];

排序包含已过期的第一个现在我想再次排序这个以及如何

1 个答案:

答案 0 :(得分:0)

更正我已采用以下方式实施您的案例

创建一个给我输入的测试方法(在你的情况下为Json Response Dictionary)

- (void)testDataForApplicationis{

NSMutableDictionary *testDict=[[NSMutableDictionary alloc]init];
NSMutableArray *testArray=[[NSMutableArray alloc]init];
[testDict setObject:@"1" forKey:@"id"];
[testDict setObject:@"Abcd" forKey:@"name_en"];
[testDict setObject:@"adfasdfasdfadsf" forKey:@"description_en"];
[testDict setObject:@"2014-01-19 00:00:00" forKey:@"start_date"];
[testDict setObject:@"2014-01-19 00:00:00" forKey:@"end_date"];
[testArray addObject:testDict];

testDict=[[NSMutableDictionary alloc]init];
[testDict setObject:@"2" forKey:@"id"];
[testDict setObject:@"Abcd" forKey:@"name_en"];
[testDict setObject:@"adfasdfasdfadsf" forKey:@"description_en"];
[testDict setObject:@"2014-01-20 00:00:00" forKey:@"start_date"];
[testDict setObject:@"2014-01-25 00:00:00" forKey:@"end_date"];
[testArray addObject:testDict];


testDict=[[NSMutableDictionary alloc]init];
[testDict setObject:@"2" forKey:@"id"];
[testDict setObject:@"Abcd" forKey:@"name_en"];
[testDict setObject:@"adfasdfasdfadsf" forKey:@"description_en"];
[testDict setObject:@"2013-12-20 00:00:00" forKey:@"start_date"];
[testDict setObject:@"2013-12-01 00:00:00" forKey:@"end_date"];
[testArray addObject:testDict];

testDict=[[NSMutableDictionary alloc]init];
[testDict setObject:@"4" forKey:@"id"];
[testDict setObject:@"Abcd" forKey:@"name_en"];
[testDict setObject:@"adfasdfasdfadsf" forKey:@"description_en"];
[testDict setObject:@"2013-08-01 00:00:00" forKey:@"start_date"];
[testDict setObject:@"2013-08-25 00:00:00" forKey:@"end_date"];
[testArray addObject:testDict];
NSMutableArray *promotionModlArray=[PromotionModel getPromotionModelObject:testArray];
 NSMutableArray *sortArray=[self sortPromotionListWithEndDate:promotionModlArray];
}

用于地图字典的模型类在对象和绑定日期以及具有一个对象的其他细节

此类名称在.h文件中 PromotionModel

@interface PromotionModel : NSObject
@property (nonatomic,strong) NSString *promotionid;
@property (nonatomic,strong) NSString *promotionName;
@property (nonatomic,strong) NSString *promotionDesc;
@property (nonatomic,strong) NSString *promotionStartDate;
@property (nonatomic,strong) NSString *promotionEndDate;

+ (NSMutableArray*)getPromotionModelObject:(NSMutableArray*)prmotionDataArray;

在通过表单测试方法

的PromotionModel .m文件映射字典中
 + (NSMutableArray*)getPromotionModelObject:(NSMutableArray*)prmotionDataArray{

NSMutableArray *prmotionDeatilArray=[[NSMutableArray alloc]init];
PromotionModel *promotionModel=nil;
for (NSMutableDictionary *promotinDetailDict in prmotionDataArray) {
    promotionModel=[[PromotionModel alloc]init];
    promotionModel.promotionid=[promotinDetailDict objectForKey:@"id"];

    promotionModel.promotionName=[promotinDetailDict objectForKey:@"name_en"];
    promotionModel.promotionDesc=[promotinDetailDict objectForKey:@"description_en"];


    promotionModel.promotionStartDate=[promotinDetailDict objectForKey:@"start_date"];
    promotionModel.promotionEndDate=[promotinDetailDict objectForKey:@"end_date"];
    [prmotionDeatilArray addObject:promotionModel];
}
    return prmotionDeatilArray;
}

此处模型类已完成

现在用相关方法对逻辑进行排序

-(NSMutableArray *)sortPromotionListWithEndDate:(NSMutableArray *)unsortedArray{
NSMutableArray *tempArray=[NSMutableArray array];
for(int i=0;i<[unsortedArray count];i++)
{
    PromotionModel *model =[unsortedArray objectAtIndex:i];

    NSString *dateStr=[self getDateinstaderdForm:model.promotionEndDate];
    NSDate *date=[self getDateFromString:dateStr];
    NSMutableDictionary *dict=[NSMutableDictionary dictionary];
    [dict setObject:model forKey:@"entity"];
    [dict setObject:date forKey:@"date"];
    [tempArray addObject:dict];
}

NSInteger counter=[tempArray count];
NSDate *compareDate;
NSInteger index;
for(int i=0;i<counter;i++)
{
    index=i;
    compareDate=[[tempArray objectAtIndex:i] objectForKey:@"date"];
    NSDate *compareDateSecond;
    for(int j=i+1;j<counter;j++)
    {
        compareDateSecond=[[tempArray objectAtIndex:j] objectForKey:@"date"];
        NSComparisonResult result = [compareDateSecond  compare:compareDate];
        if(result == NSOrderedDescending)
        {
            compareDate=compareDateSecond;
            index=j;
        }
    }
    if(i!=index)
        [tempArray exchangeObjectAtIndex:i withObjectAtIndex:index];
}


[unsortedArray removeAllObjects];
for(int i=0;i<[tempArray count];i++)
{
    [unsortedArray addObject:[[tempArray objectAtIndex:i] objectForKey:@"entity"]];
}
for (PromotionModel *model in unsortedArray) {
    NSLog(@" --------- New Object -------");
    NSLog(@"Promotion Name %@",model.promotionid);
    NSLog(@"Promotion Sort Details %@",model.promotionEndDate);
    NSLog(@"Promotion Name %@",model.promotionName);

}

return unsortedArray;

 }

在逻辑中使用的支持方法对于日期格式化。这用于将相同格式的所有日期设置为匹配。

  - (NSString*)getDateinstaderdForm:(NSString*)currDate{
NSDateFormatter *df=[[NSDateFormatter alloc]init];
[df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSDate *originalDate=[df dateFromString:currDate];
[df setDateFormat:@"yyyy-MM-dd"];
NSString *dateStr=[df stringFromDate:originalDate];
return dateStr;
 }



 -(NSDate *) getDateFromString : (NSString *)stringDate
{

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setTimeZone:[NSTimeZone timeZoneForSecondsFromGMT:0]];
//[dateFormatter setTimeZone:[NSTimeZone localTimeZone]];
//[dateFormatter setDateFormat:@"MM/dd/yyyy"];
[dateFormatter setDateFormat:@"yyyy-MM-dd"];

NSDate *date = [dateFormatter dateFromString: stringDate];
return date;
 }

我的外出

   --------- New Object -------
 2014-01-29 19:39:54.628 StackOverFlowSolutions[4756:a0b] Promotion Name 2
 2014-01-29 19:39:54.639 StackOverFlowSolutions[4756:a0b] Promotion Sort Details 2014-01-25 00:00:00
 2014-01-29 19:39:54.643 StackOverFlowSolutions[4756:a0b] Promotion Name Abcd
 2014-01-29 19:39:54.643 StackOverFlowSolutions[4756:a0b]  --------- New Object -------
 2014-01-29 19:39:54.644 StackOverFlowSolutions[4756:a0b] Promotion Name 1
 2014-01-29 19:39:54.645 StackOverFlowSolutions[4756:a0b] Promotion Sort Details        2014-01-19 00:00:00
 2014-01-29 19:39:54.645 StackOverFlowSolutions[4756:a0b] Promotion Name Abcd
 2014-01-29 19:39:54.646 StackOverFlowSolutions[4756:a0b]  --------- New Object -------
 2014-01-29 19:39:54.646 StackOverFlowSolutions[4756:a0b] Promotion Name 2
 2014-01-29 19:39:54.658 StackOverFlowSolutions[4756:a0b] Promotion Sort Details 2013-12-01 00:00:00
 2014-01-29 19:39:54.661 StackOverFlowSolutions[4756:a0b] Promotion Name Abcd
 2014-01-29 19:39:54.662 StackOverFlowSolutions[4756:a0b]  --------- New Object -------
 2014-01-29 19:39:54.663 StackOverFlowSolutions[4756:a0b] Promotion Name 4
 2014-01-29 19:39:54.663 StackOverFlowSolutions[4756:a0b] Promotion Sort Details 2013-08-25 00:00:00
 2014-01-29 19:39:54.670 StackOverFlowSolutions[4756:a0b] Promotion Name Abcd

尝试这是有效的希望这给你得到所需的输出