0

I have been struggling with what should be a fairly simple task.

In viewController.h I have:

@interface MoviesList : NSObject {    
}
@property NSString *aTitle;
@property NSDate *aReleaseYear;
@property NSString *aCharacter;
@end

In viewController.m (fetchedData contains a JSON response, movieCount is already determined) I have:

NSMutableArray *moviesArray;

MoviesList *movieDetails = [[MoviesList alloc] init];

int myCount = 0;

while (myCount < movieCount) {     
        MoviesList *movieDetails = [[MoviesList alloc] init];
        // Insert character played into array.
        movieDetails.aCharacter = fetchedData[@"cast"][myCount][@"character"];

        // Insert movie title and release date into array.
        movieDetails.aTitle = fetchedData[@"cast"][myCount][@"title"];
        movieDetails.aReleaseYear = fetchedData[@"cast"][myCount][@"release_date"];

        [moviesArray addObject:movieDetails];

        NSLog(@"Title: %@",movieDetails.aTitle);
        NSLog(@"Release date: %@",movieDetails.aReleaseYear);
        NSLog(@"Character: %@",movieDetails.aCharacter);

        myCount++;

}
NSLog(@"moviesArray: %@",moviesArray);

When I run with breakpoints and hover over 'moviesArray' in the last line above, I can see it gets populated with correct information:

@"58 objects"
[0] = (MoviesList *) 0x7962b650
> NsObject
> _aTitle = blah blah
> _aReleaseYear = blah blah
> _aCharacter = blah blah...

So it appears the array is being populated correctly. The issues now are how to sort this information by release date (I have been combing this forum all day and seen various sorting answers which I have had little success with) and then display the sorted array via NSLOG.

Any help is appreciated, there does seem a wealth of knowledge in this forum and always someone willing to offer assistance. Thank you.

1
  • Update your question with what you have attempted. Explain what issues you've had. Commented Nov 22, 2015 at 16:41

1 Answer 1

1

This snippet of code will sort the contents of moviesArray by aReleaseYear, in ascending order:

NSSortDescriptor *ageDescriptor = [[NSSortDescriptor alloc] initWithKey:@"aReleaseYear" ascending:YES];
NSArray *sortDescriptors = @[ageDescriptor];
NSArray *sortedArrayByReleaseYear = [moviesArray sortedArrayUsingDescriptors:sortDescriptors];

If you want them in descending order, change ascending to NO.

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

3 Comments

Thanks Jose, I appreciate the quick response.
Why is it not as easy as using NSLog(@"Sorted Array: %@",sortedArrayByReleaseYear); What would be the correct code to pull (for example) the first title out of sortedArrayByReleaseYear and display it via NSLOG?
If you want the first object, you can do NSLog(@"First object: %@", [sortedArrayByReleaseYear firstObject]);

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.