NSDictionary 排序

发布于 2024-11-13 04:54:32 字数 159 浏览 12 评论 0原文

我想知道是否有人可以告诉我如何对 NSDictionary 进行排序;我想从最后一个条目开始阅读它,因为键是日期+时间,并且我希望能够将其附加到 NSMutableString 中。我能够使用枚举器读取它,但没有得到我想要的结果。

谢谢

I was wondering if someone can show me how to sort an NSDictionary; I want to read it starting from the last entry, since the key is Date + Time and I want to be able to append it to an NSMutableString. I was able to read it using an enumerator but I don't get the results I want.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

疯到世界奔溃 2024-11-20 04:54:32

根据您的要求,最简单的方法是从键创建一个新数组,对其进行排序,然后使用该数组引用原始字典中的项目。

(注意 myComparison 是您自己的方法,它将比较两个键)。

NSMutableArray* tempArray = [NSMutableArray arrayWithArray:[myDict allKeys]]; 
[tempArray sortedArrayUsingSelector:@selector(myComparison:)];

for (Foo* f in tempArray)
{
  Value* val = [myDict objectForKey:f];
}

For your requirements the easiest way is to create a new array from the keys, sort that, then use the array to reference items from the original dictionary.

(Note myComparison is your own method that will compare two keys).

NSMutableArray* tempArray = [NSMutableArray arrayWithArray:[myDict allKeys]]; 
[tempArray sortedArrayUsingSelector:@selector(myComparison:)];

for (Foo* f in tempArray)
{
  Value* val = [myDict objectForKey:f];
}
等待圉鍢 2024-11-20 04:54:32

我汇总了 StackOverflow 上关于将 NSDictionary 排序为非常紧凑且按字母顺序排序的其他建议。我在“路径”中有一个 Plist 文件,我将其加载到内存中并想要转储。

NSDictionary    *glossary = [NSDictionary dictionaryWithContentsOfFile: path];
NSArray         *array1 = [[glossary allKeys] sortedArrayUsingDescriptors:@[ [NSSortDescriptor  sortDescriptorWithKey:@"" ascending:YES selector:@selector(localizedStandardCompare:)] ]];
for ( int i=0; i<array1.count; i++)
{
    NSString* key = [array1 objectAtIndex:i];
    NSString* value = [glossary objectForKey:key];
    NSLog(@"Key=%@, Value=%@", key, value );
}

I've aggregated some of the other suggestions on StackOverflow on sorting an NSDictionary into something very compact that will sort alphabetically. I have a Plist file in 'path' that I load in to memory and want to dump.

NSDictionary    *glossary = [NSDictionary dictionaryWithContentsOfFile: path];
NSArray         *array1 = [[glossary allKeys] sortedArrayUsingDescriptors:@[ [NSSortDescriptor  sortDescriptorWithKey:@"" ascending:YES selector:@selector(localizedStandardCompare:)] ]];
for ( int i=0; i<array1.count; i++)
{
    NSString* key = [array1 objectAtIndex:i];
    NSString* value = [glossary objectForKey:key];
    NSLog(@"Key=%@, Value=%@", key, value );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文