matthewojagiela matthewojagiela - 22 days ago 9
iOS Question

Sort 2 NSArrays By Date

I have 2 NSMutableArrays declared. One is filled with names and then another one is filled with string values of NSDate.

I want to sort both of them according to the date in the second one. For example if element 3 in the date array becomes element 0 I want the same to happen for the name array.

What is the easiest way to do this? I know how to sort the date array just not the corresponding name array!
(Objective-C Please!)

Answer

Sorting 2 arrays and keeping them in sync is a pain. You basically have to sort them by hand.

Probably the easiest way to do this is to create an array of dictionaries where each dictionary contains a data and a name.

Then sort the array of dictionaries by date.

EDIT:

Here is the code for creating custom objects containing a name and a date. There is code to sort by date as well as code to sort by name:

/**
 The thing class has a property date and a property name. 
 It is purely for creating sorted arrays of objects
 */
@interface Thing : NSObject

@property (nonatomic, strong) NSDate *date;
@property (nonatomic, strong) NSString *name;

@end

@implementation Thing

/**
 This is a dummy init method that creates a Thing ojbect with a random name and date
 */
- (instancetype) init {
  self = [super init];
  if (!self) return nil;

  NSString *letters = @"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  NSMutableString *temp = [NSMutableString new];
  for (int x = 0; x < 10; x++) {
    unichar aChar = [letters characterAtIndex:arc4random_uniform(26)];
    [temp appendFormat: @"%C", aChar];
  }
  self.name = [temp copy];

  //Create a random date
  uint32 halfMax = 2000000000;
  uint32 max = halfMax * 2;
  int32_t value = arc4random_uniform(max) - halfMax;
  NSTimeInterval now = [[NSDate date] timeIntervalSinceReferenceDate];
  self.date = [NSDate dateWithTimeIntervalSinceReferenceDate: now + value];
  return self;
}

- (NSString *) description {
  return [NSString stringWithFormat: @"Name: %@  Date: %@", self.name, self.date];
}

@end




int main(int argc, const char * argv[]) {
  @autoreleasepool {
    //Create an array of Thing objects
    const int count = 50;
    NSMutableArray *thingArray = [NSMutableArray arrayWithCapacity: count];
    for (int x = 0; x < count; x++) {
      thingArray[x] = [[Thing alloc] init];
    }
    #if 1
      //Sort by date, ascending
      [thingArray sortUsingComparator:^NSComparisonResult(Thing  *obj1,
                                                          Thing  *obj2) {
        NSComparisonResult bigger =  
          [obj1.date timeIntervalSinceReferenceDate] < 
          [obj2.date timeIntervalSinceReferenceDate] ?  
            NSOrderedAscending : NSOrderedDescending;
        return bigger;
      }];
    #else
      //Sort by name
      [thingArray sortUsingComparator:^NSComparisonResult(Thing  *obj1,
                                                          Thing  *obj2) {
        return [obj1.name compare: obj2.name];
      }];
    #endif
    NSLog(@"%@", thingArray);
  }
    return 0;
}