-2

Possible Duplicate:
Sort NSArray of date strings or objects

I want to sort an array which contain Dates in ascending order

Community
  • 1
  • 1
Dinesh_
  • 259
  • 1
  • 5
  • 9
  • read this....http://stackoverflow.com/questions/5820979/objective-c-sort-an-array-of-string and http://stackoverflow.com/questions/1132806/sort-nsarray-of-date-strings-or-objects – Sudha Tiwari Jan 08 '13 at 10:53
  • I don't understand why people answer to these kind of questions. – Desdenova Jan 08 '13 at 14:10

2 Answers2

2

it is a very generic way to sort any NSArray with any type of objects, using blocks. now, it just is specified as solution for your current problem with the NSDate objects.

NSArray *_arrayOfDates = // your array with the NSDate objects;
NSArray *_sortedArrayOfDates = [_arrayOfDates sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
    return [((NSDate *) obj1) compare:((NSDate *)obj2)];
}];
NSLog(@"%@", _sortedArrayOfDates);

please note: you can use NSMutableArray with -sortUsingComparator: method, which provides the same result, but the array will be sorted inside of itself.

holex
  • 23,961
  • 7
  • 62
  • 76
1

Try this:

          NSLog(@"livevideoparsingarray..%@",livevideoparsingarray);
            NSSortDescriptor * descLastname = [[NSSortDescriptor alloc] initWithKey:@"active" ascending:YES];
            [livevideoparsingarray sortUsingDescriptors:[NSArray arrayWithObjects:descLastname, nil]];
            [descLastname release];
            videoparsing = [livevideoparsingarray copy];

livevideoparsingarray is my array which I have sort & active is my tag which is in array which I have sort. You change with your requirements.

Vishal
  • 8,246
  • 6
  • 37
  • 52