I have an array of objects which all contain a expectedDeliveryDate string. I have a method which takes a date string and turns it into a NSDate.
How can I sort these in ascending or descending order?
I have an array of objects which all contain a expectedDeliveryDate string. I have a method which takes a date string and turns it into a NSDate.
How can I sort these in ascending or descending order?
I am assuming that you have an Array in which each object is Dictionary and it contains data of delivery
try this I create this method in my project to sort array with Key.
In this method lastDescriptor has selector compare: so two date will compare and sort automatically in this method.
-(NSMutableArray *)sortArrayWithKey:(NSString *)sortingKey andArray:(NSMutableArray *)unsortedArray{
NSSortDescriptor *lastDescriptor = [[NSSortDescriptor alloc] initWithKey:sortingKey ascending:NO selector:@selector(compare:)];
NSArray *descriptors = [NSArray arrayWithObjects: lastDescriptor, nil];
return [unsortedArray sortedArrayUsingDescriptors:descriptors].mutableCopy;
}
Hope this help you too..
Sorting the array with NSString date.
Things Done:
Try this
NSMutableArray *oldArray; //Array which consist of object with date as string.
NSMutableArray *newArray = [[NSMutableArray alloc] init];
if(oldArray.count!=0){
//CONVERT STRING TO DATE
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MMM d, yyyy"];
[dateFormat setTimeZone:[NSTimeZone timeZoneWithAbbreviation:@"GMT"]];
for (int i=0 ;i<oldArray.count;i++)
{
NSMutableDictionary *dict= [oldArray objectAtIndex:i];
NSString *stringDate = [dict objectForKey:@"stringDateKey"];
if(![stringDate isEqualToString:@""]){
NSDate *date = [dateFormat dateFromString:stringDate];
NSMutableDictionary *newDict = [[NSMutableDictionary alloc] init];
[newDict addEntriesFromDictionary:dict];
[newDict setObject:date forKey:@"newDate"];
[newArray addObject:newDict];
}
else{
[newArray addObject:dict];
}
}
//SORTING THE ARRAY
NSSortDescriptor *firstDescriptor = [[NSSortDescriptor alloc] initWithKey:@"newDate" ascending:NO];
NSArray *sortDescriptors = [NSArray arrayWithObjects:firstDescriptor, nil];
NSArray *sortedArray = [dataValue sortedArrayUsingDescriptors:sortDescriptors];
}
+ (void)filterAlertsByTime:(NSMutableArray*)alerts {
NSDateFormatter *hourFormatter = [[NSDateFormatter alloc] init];
[hourFormatter setDateFormat:@"MM/dd/yyyy hh:mm a"];
[alerts sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
NSDate *date1 = [hourFormatter dateFromString:[NSString stringWithFormat:@"%@",obj1[@"expectedDeliveryDate"]]];
NSDate *date2 =[hourFormatter dateFromString:[NSString stringWithFormat:@"%@",obj2[@"expectedDeliveryDate"]]] ;
return [date1 compare:date2];
}];
}
Please edit the date format according to the format of the expectedDeliveryDate string
You can use sort descriptors to sort objects by their dates. So first convert your strings to NSDates and set them on another property of your custom object, and sort them that way.
NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:@"expectedDeliveryDate"
ascending:YES];
NSArray *sortedDeliverDates = [deliveryItems sortedArrayUsingDescriptors:@[descriptor]];
Furthermore NSDate also has a compare: method that will let you compare two dates.