【发布时间】:2018-02-15 07:21:05
【问题描述】:
我想用日期对数组中的数据进行排序。我在数组索引中使用字符串格式的键“start_date”获取日期 我有以下数据数组:
[
{
city = Ahmedabad;
"start_date" = "2018-12-14";
"start_time" = "09:00:00";
},
{
city = Ahmedabad;
"start_date" = "2018-12-13";
"start_time" = "09:00:00";
},
{
city = Ahmedabad;
"start_date" = "2018-10-05";
"start_time" = "09:00:00";
},
{
city = Ahmedabad;
"start_date" = "2018-10-03";
"start_time" = "09:00:00";
},
{
city = Ahmedabad;
"start_date" = "2018-10-01";
"start_time" = "09:00:00";
},
{
city = Ahmedabad;
"start_date" = "2018-09-04";
"start_time" = "09:00:00";
},
{
city = "Subang Jaya";
"start_date" = "2018-10-15";
"start_time" = "09:00:00";
},
{
city = Kuching;
"start_date" = "2018-08-22";
"start_time" = "10:00:00";
}
]
我想使用提供开始日期字符串的键“start_date”按数组内日期的升序排序。请为我提供使用日期在数组内进行排序的建议
【问题讨论】:
-
你还没有日期,你有一个字符串。首先将您的字典 (JSON) 解析为对象。
-
@Sulthan 如何解析成对象?
-
@Sulthan 这是我使用 Alamofire 调用我的 web api 后的回应
-
您如何拥有该数据的数组?使用命名元组?
-
如果您想稍后使用其他标准对这些数据进行排序怎么办?你确定你不想像 Sulthan 所说的那样创建对象吗……然后将这些对象中的日期变量转换为 DateTime 的实例……然后让对象实现
然后你可以按照你想要的任何方式对它们进行排序。如果您接受,我会将这条评论转换为真正的答案!嘻嘻!!
标签: ios arrays swift sorting date