2

Can anyone show me how to sort an array(NSMutableArray) of NSDate in order for the actual dates?

Thanks!

2 Answers 2

4

I believe you want to use something like -NSMutableArray sortUsingSelector: and pass in @selector(compare:). Ie, assuming you had an NSMutableArray of NSDates named *dateArray*:

[dateArray sortUsingSelector:@selector(compare:)];
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks this method is useful, but for compare: selector, there is only one argument, something like compare:(NSDate *)other, but I need another date to compare.
2

Here you go just modify some part of code for your requirement

- (NSArray *)sortedWeightEntriesByWeightDate:(NSArray *)unsortedArray {

    NSMutableArray *tempArray = [NSMutableArray array];
    NSMutableArray *sortedArray = [NSMutableArray arrayWithCapacity:0];

    @try {
        for(int i = 0; i < [unsortedArray count];i++) {

            NSDateFormatter *df = [[NSDateFormatter alloc]init];
            MyDataModal *entry = [unsortedArray objectAtIndex:i];       
            [df setDateFormat:@"yyyy-MM-dd"];       
            NSDate *date = [df dateFromString:entry.weightDate];        
            NSMutableDictionary *dict = [NSMutableDictionary dictionary];

            if(date) {  
                [dict setObject:entry forKey:@"entity"];        
                [dict setObject:date forKey:@"date"];       
                [tempArray addObject:dict];
            }
            [df release];
        }

        NSInteger counter = [tempArray count];
        NSDate *compareDate;
        NSInteger index;

        for(int i = 0 ; i < counter; i++) {
            index = i;
            compareDate = [[tempArray objectAtIndex:i] valueForKey:@"date"];        
            NSDate *compareDateSecond;

            for(int j = i+1 ; j < counter; j++) {
                compareDateSecond=[[tempArray objectAtIndex:j] valueForKey:@"date"];
                NSComparisonResult result = [compareDate compare:compareDateSecond];
                if(result == NSOrderedDescending) {
                    compareDate = compareDateSecond;
                    index=j;
                }
            }
            if(i!=index)
                [tempArray exchangeObjectAtIndex:i withObjectAtIndex:index];
        }


        NSInteger counterIndex = [tempArray count];
        for(int i = 0; i < counterIndex ; i++) {
            [sortedArray addObject:[[tempArray objectAtIndex:i] valueForKey:@"entity"]];
        }

    }
    @catch (NSException * e) {
        NSLog(@"An exception occured while sorting weight entries by date");
    }
    @finally {
        return [NSArray arrayWithArray:sortedArray];
    }   
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.