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
Asked
Active
Viewed 521 times
4 Answers
0
NSArray* sortedDateList = [dateList sortedArrayUsingComparator:^(NSDate* dateOne, NSDate* dateTwo) {
return [dateOne compare:dateTwo];
}];
Thomas Zoechling
- 33,778
- 3
- 80
- 111
0
You can use NSMutableArray's method sortUsingComparator, and write your comparison block using NSDate's compare method.
Cyrille
- 24,746
- 12
- 65
- 89
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:)];
Felix Lamouroux
- 7,322
- 27
- 44
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
shivangi
- 187
- 2
- 14