gpt4 book ai didi

ios - 在 iphone 中按日期对 NSDictionary 进行排序

转载 作者:行者123 更新时间:2023-11-28 20:03:28 27 4
gpt4 key购买 nike

我有一本像这样的字典:

{
"2014-04-12" = (
{
"can_remove" = 1;
"date_created" = "12-04-2014 04:23:57";
"is_connected" = 0;
name = "J J";
status = gbn;
"user_id" = 94;
}
);
"2014-04-14" = (
{
"can_remove" = 0;
"date_created" = "14-04-2014 02:36:52";
"is_connected" = 0;
name = abc;
"user_id" = 89;
}
);
}

这个字典的键代表日期。如何按日期排序?

我试过了,但是没用:

NSArray* keys = [dict allKeys];
NSArray* sortedArray = [keys sortedArrayUsingComparator:^(id a, id b) {
return [a compare:b options:NSNumericSearch];
}];

最佳答案

使用以下方式根据日期对字典进行排序:

NSMutableArray *dictKeys = [[dict allValues] mutableCopy];

[dictKeys sortUsingComparator: (NSComparator)^(NSDictionary *a, NSDictionary *b)
{
NSString *key1 = [a objectForKey: @"field3"];
NSString *key2 = [b objectForKey: @"field3"];

return [key1 compare: key2];
}
];

NSLog (@"dictKeys - %@",dictKeys);

关于ios - 在 iphone 中按日期对 NSDictionary 进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23056886/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com