简体   繁体   中英

Reverse NSMutableDictionary

I'm just learning objective-c and I have an NSMutableDictionary with UITableViewCell sections in it. The thing is, it's sorted on year and I'd like to reverse that order so the last year comes on top and not bottom.

Any ideas how to reverse sort an NSMutableDictionary?

Thanks in advance

As already stated, an NSDictionary has no order.

What you can do is order the keys in a separate array and then access the keyed objects in the dictionary in the order that the keys appear in your ordered array.

Something like this:

NSArray *orderedKeys = [[dictionary allKeys] sortedArrayUsingDescriptors:<your sort descriptor>];
for (NSString *key in orderedKeys)
{
    MyObject *myObject = [dictionary objectForKey:key];
    // do what you will with myObject
}

As already pointed out, a dictionary does not guarantee you any order. You might be interested in this related question that shows how to get all dictionary values sorted using a custom function.

NSDictionary没有隐含顺序。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM