gpt4 book ai didi

ios - 如何根据TimeWise对数组进行排序?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:02:28 25 4
gpt4 key购买 nike

我想根据时间的 AM 和 PM 值对数组进行排序...

我收到如下回复:

(
{
Rate = "4.44";
Count = 3;
Hours = "04:00 PM";
},
{

Rate = "5.55";
Count = 5;
Hours = "07:00 AM";
},
{
Rate = "3.33";
Count = 2;
Hours = "08:00 PM";
}
)

我想根据上午和下午对时间进行排序以显示时间。

最佳答案

首先需要将 Hours 字符串转换为 NSDate 类的对象:

NSArray *sorted = [array sortedArrayUsingComparator:^NSComparisonResult(NSDictionary *a, NSDictionary *b) {
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateFormat = @"hh:mm a";
df.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];

NSDate *dateA = [df dateFromString:[a objectForKey:@"Hours"]];
NSDate *dateB = [df dateFromString:[b objectForKey:@"Hours"]];

return [dateA compare:dateB];
}];

关于ios - 如何根据TimeWise对数组进行排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20284404/

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