0

I need to sort rows by due dates from XML:

<plist version="1.0">
<array>
<array>
    <string>23 Jan 2012</string>
    <string>Chinese New Year</string>
    <string>Closed</string>
</array>
<array>
    <string>24 Jan 2012</string>
    <string>Chinese New Year</string>
    <string>Closed</string>
</array>
</array>

I have studied the answer given in : Objects Sorting With date ,Time Problem in Array(Iphone Development) which i found very useful but i'm in doubt:

  1. I don't understand where do i get the @"entity" and @"date" . Is it a variable or id and where do i obtain it?

    if (date) {
    [dict setObject:entry forKey:@"entity"];
    [dict setObject:date forKey:@"date"];
    [tempArray addObject:dict]; }

  2. The month given is in a MMM format (JAN,FEB,MAR), which is fixed as one of my project scope and i clueless about how am I able to sort by the abbreviation as compared to the normal numbered ones.

Would deeply appreciate some help here as I'm so stuck with this for a few days!! Thanks!

Community
  • 1
  • 1

2 Answers2

0

make a class(YourClass) with objects (yourDateString, yourEvent, yourStatus)

YourClass
--yourDateString
--yourEvent
--yourStatus

parse your xml save details in object of this class (YourClass) and store the objects in this array(arrayWithYourContents) using

    NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setTimeStyle:NSDateFormatterFullStyle];
    [dateFormatter setDateFormat:@"dd mmm yyyy"];

    arrayWithYourContents sortUsingComparator: ^(id dateObj1, id dateObj2) 
     {
         NSDate *date1 = [dateFormatter dateFromString:[(YourClass *) dateObj1 yourDateString]];


         NSDate *date2 = [dateFormatter dateFromString:[(YourClass *) dateObj2 yourDateString]];

         return [date2 compare: date1];
     }
     ];  

I am not sure about the date format (@"dd mmm yyyy").. please check if this works or change it to which matches to yours.

arrayWithYourContents shall now contain your sorted date components.

Zaraki
  • 3,720
  • 33
  • 39
  • Ah this looks more understandable then other answers i saw and is trying it. Thanks much! :D –  Jul 16 '12 at 08:55
0

Why do you need MMM month format ? a "20120131" 'YYYYmmdd" date format is ordered so is more easy to sort and translate back to readable date.

Your xml is an array of array, you load this file using NSArray arrayWithContentsOfFile for free.

Personally, I'ld use an array of dictionary as tag order is not garanty by xml standard (if i remember well).

a reading here about xml ordering : http://www.ibm.com/developerworks/xml/library/x-eleord/index.html

divol
  • 192
  • 7
  • The date format was given as a fixed scope unfortunately :( and I was wondering why it was given this way but I can only stick with it though. –  Jul 16 '12 at 08:54