Sorting NSMutableArray by date

I want to sort a mutable array by date. My array contains several dict with keys say:key1,key2,birthday.Now, I have to sort by its birthday key: I know that this can be done using:

NSSortDescriptor *descriptor = [[NSSortDescriptor alloc] initWithKey:@"birthday" ascending:YES]; 
[myArray sortUsingDescriptors:[NSArray arrayWithObjects:descriptor,nil]];

But my problem is that I want to sort only those arrays, which don’t contain empty birthday field. My array will contains several empty birthday fields. I don’t want to sort those. Finally I have to load these in table view through [self.mTable reloadData];.


First collect the indices of all objetcs without a birthday.

NSIndexSet *indexSet = [NSIndexSet indexSet];
[array enumerateObjectsUsingBlock:^(NSDictionary *dict, NSUInteger idx, BOOL *stop)
    if(![[dict allKeys] containsObject:@"birthday"]){
        [indexSet addIndex:idx];

Now remove them from the original array

[array removeObjectsAtIndexes:indexSet];

Using a comparator block, sorting could look like

[array sortUsingComparator: ^(NSDictionary *d1, NSDictionary *d2) {    
    NSDate *date1 = [d1 objectForKey:@"birthday"];
    NSDate *date2 = [d2 objectForKey:@"birthday"];

    return [date1 compare:date2]

Create a different array to back your table view like this:

NSDictionary* obj1 = [NSDictionary dictionaryWithObject: [NSDate date] forKey: @"birthday"];
NSDictionary* obj2 = [NSDictionary dictionaryWithObject: [NSDate dateWithTimeIntervalSince1970: 0] forKey: @"birthday"];
NSDictionary* obj3 = [NSDictionary dictionaryWithObject: @"wow" forKey: @"no_birthday"];

NSArray* all = [NSArray arrayWithObjects: obj1, obj2, obj3, nil];
NSArray* onlyWithBirthday = [all valueForKeyPath: @"@unionOfObjects.birthday"];

And if you need the full objects for the table view, continue with this code:

NSPredicate* filter = [NSPredicate predicateWithFormat: @"SELF.birthday IN %@", onlyWithBirthday];
NSArray* datasource = [all filteredArrayUsingPredicate: filter];

Then you can apply your sort method of choice.

Need Your Help

RhinoMocks - Mocking Protected virtual members

mocking rhino-mocks

As far as I know, RM should mock anything that can be accessed by a derived class. Since a protected virtual member is accessible from a subclass, shouldn't it be mockable?

How I can avoid "Element is not currently visible and so may not be interacted with " Selenium Webdriver

selenium webdriver selenium-webdriver

Am using selenium webdriver 2.210 + JAVA for testing.I have a sample code for selecting all mails in gmail.But the code throws an "Element is not currently visible and so may not be interacted with"