简体   繁体   中英

sort NSMutableDictionary contain object from Json with date string

I am having one mutable-dictionary with date key and name key Date key have date in three manner one- expired date,two- upcoming date and nil (0000-00-00 YYYY-MM-DD) . now I wants to sort this NSDictionary array in order . Upcoming date (ascending order). Expired Date. Other may be nil I used comparator and sort array in 2 format one array without null value and one is with value and append these array but problem with its not showing with array value in ascending order expired show first...

  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];


    }

}

unfiltered array have value mixing of present date in 2013 year and 2014 so 2013 value are expired and 2014 value are available so i need to sort 2014 value ascending order and expired will be in another array .

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

sorted contain expired first now i want to sort again this and how

Correction I have implemented Your case with following way

Create One Test Methods that give me input (In your case 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];
}

Model Class for map Dictionary in Object and bind date and othere detail with one Object

Here Class name is PromotionModel here in .h file

@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;

In PromotionModel .m File Map Dictionary that pass form test method

 + (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;
}

Here model Class Completed

Now Here Sorting Logic With Related Methods

-(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;

 }

Suported Method that use in logic For date formate.This is use to set All date in same format for match.

  - (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;
 }

My Out Put

   --------- 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

Try this this is working Hope This give you get desired OUTPUT

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM