0

I have a NSArray of AEMEvent custom objects. One of the properties of this AEMEvent class is startDate of type NSDate.

@interface AEMEvent : NSObject
{
    //.....
    NSDate *startDate;
    //..........
}

I would like to sort this array based on this property, so the output will be an NSArray full of AEMEvent objects ordered by startDate attribute.

I have seen this interesting answer for sorting an NSArray of NSDate objects using -[NSArray sortedArrayUsingSelector: or -[NSMutableArray sortUsingSelector:] and passing @selector(compare:)as parameter and wonder if there will be a way to use this in my situation - Sort NSArray of date strings or objects

Community
  • 1
  • 1
David Casillas
  • 1,801
  • 1
  • 29
  • 57

1 Answers1

1
// Sort AMEvent by startDate 

NSSortDescriptor * firstDescriptor = [[[NSSortDescriptor alloc] 
                                       initWithKey:@"startDate" ascending:YES 
                                       selector:@selector(caseInsensitiveCompare:)]

NSArray * descriptors = [NSArray arrayWithObjects:firstDescriptor, nil];  
NSArray * sortedArray = [yourArray sortedArrayUsingDescriptors:descriptors];
Ecarrion
  • 4,940
  • 1
  • 33
  • 44