1

I have an array of dates that are adding one by one to the table view , but i want to sort them in ascending order irrespective of the sequence added them,can any one help me

1

4 Answers 4

0

You can use NSMutableArray's method sortUsingComparator, and write your comparison block using NSDate's compare method.

Sign up to request clarification or add additional context in comments.

Comments

0

If you are using NSDate object this should be easy:

NSDate *date1 = ...;
NSDate *date2 = ...;
NSDate *date3 = ...;

NSArray *dates = [NSArray arrayWithObjects:date1,date2,date3,nil];

NSArray *sortedArray = [dates [anArray sortedArrayUsingSelector:@selector(compare:)];

Comments

0
NSArray* sortedDateList = [dateList sortedArrayUsingComparator:^(NSDate* dateOne, NSDate* dateTwo) {
    return [dateOne compare:dateTwo];
}];

1 Comment

return [dateOne compare:dateTwo] would be better.
0
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"beginDate" ascending:TRUE];
[myMutableArray sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
[sortDescriptor release];

Use this sort descriptor for sorting array of dates

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.