2

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?

2
  • Write the appropriate comparator and sort. Did you have a specific question? Commented May 2, 2015 at 2:42
  • 2
    Update your question with some relevant code. Show what you tried and what issues you are having. Commented May 2, 2015 at 3:18

4 Answers 4

2

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..

Sign up to request clarification or add additional context in comments.

1 Comment

Check out the question it convert the string to NSDate before sorting so it will work i guess
2

Sorting the array with NSString date.

Things Done:

  1. Convert the array which has NSString Date to NSDate
  2. Then sort the array with NSDate.

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];

}

Comments

0
+ (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

2 Comments

What's the point of the currentDateFormatter and currentDateString?
Now you can get rid of the needless stringWithFormat calls.
-1

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.

1 Comment

The OP said they are date strings, not NSDate objects.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.