0

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

  • 3
    NSDictionary is a cocoa container that is not sortable. – Desdenova Jan 28 '14 at 11:24
  • i have json object in nsmutable dictionary and just transfer in nsarray please check edited question – user2638242 Jan 28 '14 at 11:27
  • @Desdenova , while this is true, there are ways to sort the values of the dictionary by copying its values to an array or something like that. – Hless Jan 28 '14 at 11:27
  • convert to array then sort – codercat Jan 28 '14 at 11:28
  • @user2638242 http://stackoverflow.com/questions/4558639/sort-an-nsmutabledictionary you'd have to go with something like the top answer of that question. – Hless Jan 28 '14 at 11:29
  • @Hless right.. but now its affecting my name key if i sort individual array can we sort total key value in nsarray? – user2638242 Jan 28 '14 at 11:31
  • You could copy all dictionary values to an array in the first place, eg: `[{name: "Ex1", date: "date"}, {name: "Ex2", date: "date"}]` and then sort that array by subkey date. – Hless Jan 28 '14 at 11:33
  • i tried and now i am able to get sorted value but problem is i getting expired value first then remain but i need without expired date value even ascending :YES is also not showing ascending result – user2638242 Jan 28 '14 at 11:39

1 Answers1

0

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

user1548843
  • 670
  • 12
  • 20
  • i have 8 key not only name and date its 8 and i have to sort this in NSUrlConnection method did Finish only. – user2638242 Jan 28 '14 at 12:08
  • @user2638242 You need to map your dictionary to model object edit code for that. – user1548843 Jan 28 '14 at 12:33
  • please tell me meaning of MAPromotionModel, MAObject? both are your controller iam calling method using NSMutableArray *dataArray= [pramotion getPromotionModelObject:unfiltered]; here pramotion is my class – user2638242 Jan 28 '14 at 13:03
  • yes,PromotionModel is my NSObject type class it conent static (+)method GetPromotionModelObject Please check updated code – user1548843 Jan 29 '14 at 06:29
  • Dear i got result but not as in manner that i want its, "2014-04-24","2013-09-20","2013-12-05","2013-09-18","2013-09-20","2013-09-27","2013-09-20","2013-09-18","2014-01-01","2013-12-10","2013-09-19","2013-11-18","2013-11-22","2013-11-18","2014-01-20","2014-02-04" i am expecting like 2014-02-04","2014-04-24"2014-01-20 Upcoming date ascending order then Expire Date – user2638242 Jan 29 '14 at 07:27