开发者

Sort array by one key then another key?

开发者 https://www.devze.com 2023-01-25 07:33 出处:网络
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.

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 开发者_如何学Python(keeping the year structure) and then by day (keeping the year and month structure).

Thanks


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

精彩评论

暂无评论...
验证码 换一张
取 消