0

basically I have an array of dictionaries and each dictionary has a day, month and year field.

I want to be able to sort the array by year, then by month, then by day. As if sorting a date.

Is this possible. This is what I have at the moment to sort by "date" as a whole:

NSSortDescriptor *aSortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:NO];
[winesOfTheWeek sortUsingDescriptors:[NSArray arrayWithObject:aSortDescriptor]];

Is there any way to do it three times so it sorts it by year, then by month (keeping the year structure) and then by day (keeping the year and month structure).

Thanks

1 Answer 1

4
NSSortDescriptor *yearSortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"year" ascending:NO];
NSSortDescriptor *monthSortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"month" ascending:NO];
NSSortDescriptor *daySortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"day" ascending:NO];
[winesOfTheWeek sortUsingDescriptors:[NSArray arrayWithObjects:yearSortDescriptor, monthSortDescriptor, daySortDescriptor, nil]];
[daySortDescriptor release];
[monthSortDescriptor release];
[yearSortDescriptor release];
Sign up to request clarification or add additional context in comments.

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.