Sort NSArray strings or date objects

I have an NSArray that contains a string of strings (i.e. NSString) as follows: "Thu, May 21, 09 19:10:09 -0700"

I need to sort NSArray by date. I thought about converting a date string to an NSDate object NSDate first, but stuck there on how to sort an NSDate object.

Thank.

+82
sorting objective-c cocoa nsdate nsarray
Jul 15 '09 at 17:33
source share
9 answers

Save the dates as NSDate objects in an NS (Mutable) Array, then use -[NSArray sortedArrayUsingSelector: or -[NSMutableArray sortUsingSelector:] and pass @selector(compare:) as a parameter. The method -[NSDate compare:] arrange the dates in ascending order. This is simpler than creating an NSSortDescriptor , and much simpler than writing your own comparison function. ( NSDate objects know how to compare themselves to each other, at least as efficiently as we could hope to achieve with custom code.)

+109
Jul 15 '09 at 21:12
source share

If I have an NSMutableArray objects with a "beginDate" field of type NSDate , I use NSSortDescriptor , as shown below:

 NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"beginDate" ascending:TRUE]; [myMutableArray sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]]; [sortDescriptor release]; 
+113
Oct 12 '10 at 9:25
source share

You can also use something like the following:

 //Sort the array of items by date [self.items sortUsingComparator:^NSComparisonResult(id obj1, id obj2){ return [obj2.date compare:obj1.date]; }]; 

But this assumes that the date is saved as NSDate , not NString , which should not be a problem for make / do. Preferably, I recommend also storing the data in it in a raw format. Easier to manipulate in such situations.

+16
Nov 10 '12 at 18:29
source share

You can use blocks to sort in place:

 sortedDatesArray = [[unsortedDatesArray sortedArrayUsingComparator: ^(id a, id b) { NSDate *d1 = [NSDate dateWithString: s1]; NSDate *d2 = [NSDate dateWithString: s2]; return [d1 compare: d2]; }]; 

I suggest you convert all of your strings to dates before sorting so as not to make more conversions than there are date elements. Any sorting algorithm will give you more row-by-date conversions than the number of elements in the array (sometimes significantly more)

a bit more on sorting blocks: http://sokol8.blogspot.com/2011/04/sorting-nsarray-with-blocks.html

+9
Apr 14 2018-11-11T00:
source share

You can use sortedArrayUsingFunction:context: Here is a sample:

 NSComparisonResult dateSort(NSString *s1, NSString *s2, void *context) { NSDate *d1 = [NSDate dateWithString:s1]; NSDate *d2 = [NSDate dateWithString:s2]; return [d1 compare:d2]; } NSArray *sorted = [unsorted sortedArrayUsingFunction:dateSort context:nil]; 

When using NSMutableArray you can use sortArrayUsingFunction:context: instead.

+5
Jul 15 '09 at 18:00
source share

In my case, it worked:

  NSArray * aUnsorted = [dataToDb allKeys];
     NSArray * arrKeys = [aUnsorted sortedArrayUsingComparator: ^ NSComparisonResult (id obj1, id obj2) {
         NSDateFormatter * df = [[NSDateFormatter alloc] init];
         [df setDateFormat: @ "dd-MM-yyyy"];
         NSDate * d1 = [df dateFromString: (NSString *) obj1];
         NSDate * d2 = [df dateFromString: (NSString *) obj2];
         return [d1 compare: d2];
     }]; 

I had a dictionary, where are all the keys, where are the dates in dd-MM-yyyy format. And allKeys returns the dictionary keys unsorted, and I wanted to present the data in chronological order.

+5
Jan 16 '15 at 20:16
source share

Once you have NSDate , you can create an NSSortDescriptor using initWithKey:ascending: and then use sortedArrayUsingDescriptors: to sort.

+4
Jul 15 '09 at 17:52
source share

Change it

 NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"beginDate" ascending:TRUE]; [myMutableArray sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]]; [sortDescriptor release]; 

For

 NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"Date" ascending:TRUE]; [myMutableArray sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]]; [sortDescriptor release]; 

Just change the KEY: it should be Date always

+1
May 01 '13 at 12:46
source share

Swift 3.0

 myMutableArray = myMutableArray.sorted(by: { $0.date.compare($1.date) == ComparisonResult.orderedAscending }) 
+1
Sep 20 '17 at 5:56 on
source share



All Articles