开发者

How to sort an array in descending order which contains the date value string in it in Iphone sdk?

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.


Sort by date according data.. date month and year.

NSMutableArray *objArray=[[NSMutableArray alloc]init];

[objArray addObject:@"18/01/2013"];
[objArray addObject:@"16/01/2013"];
[objArray addObject:@"15/01/2011"];
[objArray addObject:@"17/01/2013"];
[objArray addObject:@"03/02/2012"];
[objArray addObject:@"05-01-2014"];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init]; [formatter setDateFormat:@"dd/MM/yyyy"];
NSComparator compareDates = ^(id string1, id string2)
{
    NSDate *date1 = [formatter dateFromString:string1];
    NSDate *date2 = [formatter dateFromString:string2];

    return [date1 compare:date2];
};

NSSortDescriptor * sortDesc = [[NSSortDescriptor alloc] initWithKey:@"self" ascending:YES comparator:compareDates];

[objArray sortUsingDescriptors:[NSArray arrayWithObject:sortDesc]];

NSLog(@"%@",objArray);*/


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜