简体   繁体   English

Objective-C字典键按降序排序

[英]Objective-C Dictionary keys sorting in descending order

I have an unordered dictionary like this: 我有这样的无序词典:

@{ @"20101R" : @"value1", 
   @"20102R" : @"value2", 
   @"20131R" : @"value3", 
   @"20111R" : @"value4", 
   @"20122R" : @"value5" }

Each key represents Year and either 1R or 2R . 每个键代表Year1R2R

For UITableView, I wish to put 'keys' as title and 'values' as subtitle like this: 对于UITableView,我希望将“键”作为标题,将“值”作为字幕,如下所示:

from latest key value -> oldest key value 最新键值开始-> 最旧键值

title : 20131R
subtitle : value3

title : 20122R
subtitle : value5

title : 20111R
subtitle : value4

title : 20102R
subtitle : value2

title : 20101R
subtitle : value1

Can you help me sort this number-like string in descending order? 您能帮我按降序对这个类似数字的字符串进行排序吗? What is the simplest way to do this? 最简单的方法是什么? Thank you in advance. 先感谢您。

Pull out the keys with 用拉出钥匙

NSArray *keysArray = [dictionary allKeys];

Then sort the array: 然后对数组进行排序:

NSArray *sortedArray = [keysArray sortedArrayUsingComparator:^NSComparisonResult(NSString*  _Nonnull obj1, NSString*  _Nonnull obj2) {
    return obj1 < obj2;
}];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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