tags:

views:

26

answers:

1

Hi guy's

I am having an string array which contains the date values as : 2010-09-18,2010-09-23,2010-09-27. I need to sort them in descending order,can anyone please help me to do this.

like 2010-09-27,2010-09-23,2010-09-18.

Actually what I am doing was:

 -(void) organizeAisleItemsIntoIndexes
{
printf("\n Inside organizeAisleItemsIntoIndexes methos of WineNameController,,,!!");
[masterAisleItemListDictionary release];
masterAisleItemListDictionary = [[NSMutableDictionary alloc] init];
CustomerDetails *currentElement;

for ( currentElement in filteredListCount)
{
    NSArray *timeStampArray = [currentElement.timeStamp componentsSeparatedByString:@" "];
    NSString *dateStr = [timeStampArray objectAtIndex:0];
    printf("\n dateStr....%s",[dateStr UTF8String]);
    NSMutableArray *indexArray = [masterAisleItemListDictionary objectForKey:dateStr];
    if (indexArray == nil)
    {
        indexArray = [[NSMutableArray alloc] init];
        [masterAisleItemListDictionary setObject:indexArray forKey:dateStr];
        [indexArray release];
    }
    [indexArray addObject:currentElement];
}
masterAisleItemListIndexArray = (NSMutableArray*)[ [masterAisleItemListDictionary allKeys] sortedArrayUsingSelector:@selector(compare:)];
 }

Anyone's help will be much appreciated.

Thank's for all,

Lakshmi.

A: 

I would set up an NSDateFormatter and convert those strings to Dates.

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"YYYY-MM-dd"];

Then I would use the NSDate compare method to order them and use a traditional sort algorithm.

NSDate *first = [formatter dateFromString:string1];
NSDate *second = [formatter dateFromString:string2];
switch ([first compare:second])
{
    case NSOrderedSame:
        break;
    case NSOrderedAscending:
        break;
    case NSOrderedDescending:
        break;
}

[edit]

Or... try putting your stuff in a list and do this:

NSSortDescriptor *dateSortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"date" ascending:NO selector:@selector(compare:)] autorelease];
[list sortUsingDescriptors:[NSArray arrayWithObjects:dateSortDescriptor, nil]];

but I confess to not knowing much about NSSortDescriptor.


[EDIT 2]

You need to put the information in a Dictionary and then all the dictionaries into an array, and then you can sort that array.

This guy gives a great example.

Stephen Furlani
Yeah I tried it but I lost.What is the key value here?
Laxmi G
added a second edit.
Stephen Furlani