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