2

Can you help me on sorting (ascending order) the array contains NSDate.

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

[tempArray addObject: NSDate1];
[tempArray addObject: NSDate2];
[tempArray addObject: NSDate3];
[tempArray addObject: NSDate4];
Dan Loewenherz
  • 10,879
  • 7
  • 50
  • 81
Bharath
  • 190
  • 7
  • 2
    You should do something like this : http://stackoverflow.com/a/8136413/1915443 – dosdos Dec 29 '15 at 09:42
  • Thanks so much, it works fine – Bharath Dec 29 '15 at 09:46
  • Check out these links : [link 1](http://stackoverflow.com/questions/4354753/sorting-an-nsarray-of-nsdates) [link2](http://stackoverflow.com/questions/12913377/sort-array-of-objects-by-their-nsdate-property) [link 3](http://stackoverflow.com/questions/6083408/sort-nsarray-of-custom-objects-by-their-nsdate-properties) [Google search](https://www.google.co.in/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8#q=sort%20nsdate%20array) – nikhil84 Dec 29 '15 at 09:46
  • http://stackoverflow.com/questions/11374040/objective-c-sort-string-date-array – Mehul Sojitra Dec 29 '15 at 09:46
  • Possible duplicate of [Sorting NSDates](http://stackoverflow.com/questions/8136367/sorting-nsdates) – Markus Kull Dec 29 '15 at 09:57

5 Answers5

1

Since NSDate responds to compare and the default sorting order is ascending, the simplest way is

[tempArray sortUsingSelector:@selector(compare:)];
vadian
  • 274,689
  • 30
  • 353
  • 361
1

Use below code:

  [your array sortUsingComparator:
  ^NSComparisonResult(id obj1, id obj2){

         obj1 *o1 = (obj1*)obj1;
         obj2 *o2 = (obj2*)obj2;
         if (o1.personAge > 02.personAge) {
            return (NSComparisonResult)NSOrderedDescending;
        }


        return (NSComparisonResult)NSOrderedSame;
}

];

0
NSSortDescriptor *dateSortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"self.dueDate" ascending:YES comparator:^NSComparisonResult(id obj1, id obj2) {

    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"dd/MM/yyyy"];
    NSDate *date1 = [dateFormat dateFromString:obj1];
    NSDate *date2 = [dateFormat dateFromString:obj2];

    return [date1 compare:date2];
}];
Koen.
  • 25,449
  • 7
  • 83
  • 78
SHASHANK
  • 109
  • 2
  • 13
0

Best way to do this,use sort descriptor.

 NSSortDescriptor *dateSortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"self.dueDate" ascending:YES comparator:^NSComparisonResult(NSDate *dateObject1, NSDate *dateObject2) {

return [dateObject1 compare:dateObject2];
}];
Avinash Tag
  • 162
  • 2
  • 12
  • That sorts actually strings (NSString) rather than dates (NSDate) – vadian Dec 29 '15 at 11:36
  • No @vadian we converting the date string to date and then comparing the dates. If you already have two dates then just compare it date1 compare:date2 – Avinash Tag Dec 30 '15 at 04:09
  • The question **is** about `NSDate` objects. – vadian Dec 30 '15 at 05:15
  • @vadian i edited the method please check let me know the problem if not done by this snippet of code. – Avinash Tag Dec 30 '15 at 05:23
  • Basically it's correct now. But the question is also about sorting an `NSMutableArray` of dates rather than dictionaries or custom objects with a key `dueDate`. – vadian Dec 30 '15 at 05:28
0
[array1 sortUsingComparator: (NSComparator)^(NSString *key1, NSString *key2)
 {        
     return [key2 compare: key1];
 }
 ];

If you want array in ascending or descending order just change

compare

return [key1 compare: key2];
Viraj Padsala
  • 1,388
  • 1
  • 13
  • 31