-6

I want to sort array using Year wise and create year wise array and create whole main Array anybody help me?

NSArray *anArray = @ @{@"newsID" : @"1",@"userID" : @"3",@"catID" : @"2",@"news" : @"may i know",@"date" : @"Tue, 22 May 2013 00:00:00 GMT+05:30"},];
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • arrays are not the place to hold this much information, it very much sounds like you need to get yourself a database instead! And whatever that code is supposed to be, its not an array! – luk2302 May 17 '15 at 12:23
  • I think he missed @[ from starting of array it should be something like this NSArray *anArray = @[ @{@"newsID",... – ShujatAli May 17 '15 at 12:34
  • It would be better if you dumb the data into database , and query the data by date, if you have large amount of data – ShujatAli May 17 '15 at 12:37
  • First you need to learn the difference between an array and a dictionary!! And what is the extra "@" there for?? – Hot Licks May 17 '15 at 12:52

1 Answers1

3

You can use sort descriptors for sorting

NSArray *sortedArrayWithDate = [anArray sortedArrayUsingComparator:
                                        ^(id obj1, id obj2) {
                                            NSString  *dt1string = [obj1 objectForKey:@"date"];
                                            NSString *dt2string = [obj2 objectForKey:@"date"];
                                            NSDate *dt1 =[self converttoDate : dt1string];
                                            NSDate *dt2 =[self converttoDate : dt2string];
                                            return [dt1 compare:dt2];

                                        }];

you have to create function for converttoDate that takes string as parameter returns date you can easily find functions for converting date to string according to date format

References for stringtodate

[http://stackoverflow.com/questions/4380381/convert-string-to-date-in-my-iphone-app][1]
[http://stackoverflow.com/questions/936969/nsdate-and-nsdateformatter-short-format-date-and-time-in-iphone-sdk][2]
ShujatAli
  • 1,376
  • 2
  • 14
  • 26
  • I think the second date string meant to refer to `obj2`. Also, as a minor refinement, make sure that `convertToDate` doesn't instantiate a new date formatter each time, as that's a relative expensive process. Cache it. – Rob May 17 '15 at 12:19
  • Except what the OP has is a dictionary. – Hot Licks May 17 '15 at 12:53
  • @HotLicks - I suspect he really has an array of dictionaries, but omitted the opening `@[`. You'll see the closing `]` at the end. I suspect an editing error in the process of cutting out extraneous portions of his sample to keep it more concise. The title, the text, and the extra `@` make it clear that he intended to specify an array. – Rob May 17 '15 at 13:07
  • @Rob - It's incredibly common for folks here to attempt to use dictionaries like arrays and vice-versa. – Hot Licks May 17 '15 at 13:08
  • @HotLicks - Yeah, I know, but IMHO the floating `@` at the start and extra closing `]` make it equally likely that it was a simple editing error. I wouldn't necessarily jump to the conclusion that the OP was oblivious to the difference between dictionaries and arrays. – Rob May 17 '15 at 13:13