如何对 Swift 中的日期组件进行排序?
How do I sort date components in Swift?
我想按日期分组数据,其初始格式为 yyyy-MM-dd'T'HH:mm:ss.SSSZ
并显示为 E, dd MMM
。我已经设法按日期对它们进行分组,但未能按降序对日期进行排序。如何在字典中分组后对日期组件进行排序?
JSON 回应
{
"list": [
{
"userId": "test1",
"transactionTime": "2019-06-20T14:01:00.253+08:00"
},
{
"userId": "test2",
"transactionTime": "2019-06-16T14:02:00.253+08:00"
},
{
"userId": "test3",
"transactionTime": "2019-06-12T14:01:00.253+08:00"
},
{
"userId": "tes4",
"transactionTime": "2019-06-17T14:02:00.253+08:00"
},
]
}
分组
func convertToDateObj() -> Date {
let dateFormatter = DateFormatter()
// Convert from initial date string to date object
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
let dateObj = dateFormatter.date(from: self)!
return dateObj
}
// Group list by date
let groupedList = Dictionary(grouping: rawTransactionsList, by: { list -> DateComponents in
let dateObj = list.transactionTime.convertToDateObj()
let date = Calendar.current.dateComponents([.day, .month], from: (dateObj))
return date
})
// QUESTION
// How do I sort the keys?
// groupList.keys.sorted(...)?
// Populate my list with grouped list
groupedList.keys.forEach { key in
print("Group keys \(key)")
let values = groupedList[key]
groupedtransactionsList.append(values ?? [])
}
你也应该按年份分组,否则不同年份的同一天会在同一组:
let date = Calendar.current.dateComponents([.day, .month, .year], from: (dateObj))
对键进行排序的一种方法是使用 Calendar.current.date(from:)
:
将日期组件转换回 Date
s
let sortedList = groupedList.sorted {
Calendar.current.date(from: [=11=].key) ?? Date.distantFuture <
Calendar.current.date(from: .key) ?? Date.distantFuture
}
sortedList.forEach { key, values in
print("Group keys \(key)")
groupedtransactionsList.append(values ?? [])
}
我的建议是使用格式为 yyyy-MM-dd
的日期字符串作为字典键而不是日期组件。这个字符串可以排序。
let groupedList = Dictionary(grouping: rawTransactionsList, by: { list -> String in
let dateObj = list.transactionTime.convertToDateObj()
let comps = Calendar.current.dateComponents([.day, .month, .year], from: dateObj)
return String(format: "%ld-%.2ld-%.2ld", comps.year!, comps.month!, comps.day!)
})
groupedtransactionsList = groupedList.keys.sorted(by: >).map { groupedList[[=10=]]! }
我想按日期分组数据,其初始格式为 yyyy-MM-dd'T'HH:mm:ss.SSSZ
并显示为 E, dd MMM
。我已经设法按日期对它们进行分组,但未能按降序对日期进行排序。如何在字典中分组后对日期组件进行排序?
JSON 回应
{
"list": [
{
"userId": "test1",
"transactionTime": "2019-06-20T14:01:00.253+08:00"
},
{
"userId": "test2",
"transactionTime": "2019-06-16T14:02:00.253+08:00"
},
{
"userId": "test3",
"transactionTime": "2019-06-12T14:01:00.253+08:00"
},
{
"userId": "tes4",
"transactionTime": "2019-06-17T14:02:00.253+08:00"
},
]
}
分组
func convertToDateObj() -> Date {
let dateFormatter = DateFormatter()
// Convert from initial date string to date object
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
let dateObj = dateFormatter.date(from: self)!
return dateObj
}
// Group list by date
let groupedList = Dictionary(grouping: rawTransactionsList, by: { list -> DateComponents in
let dateObj = list.transactionTime.convertToDateObj()
let date = Calendar.current.dateComponents([.day, .month], from: (dateObj))
return date
})
// QUESTION
// How do I sort the keys?
// groupList.keys.sorted(...)?
// Populate my list with grouped list
groupedList.keys.forEach { key in
print("Group keys \(key)")
let values = groupedList[key]
groupedtransactionsList.append(values ?? [])
}
你也应该按年份分组,否则不同年份的同一天会在同一组:
let date = Calendar.current.dateComponents([.day, .month, .year], from: (dateObj))
对键进行排序的一种方法是使用 Calendar.current.date(from:)
:
Date
s
let sortedList = groupedList.sorted {
Calendar.current.date(from: [=11=].key) ?? Date.distantFuture <
Calendar.current.date(from: .key) ?? Date.distantFuture
}
sortedList.forEach { key, values in
print("Group keys \(key)")
groupedtransactionsList.append(values ?? [])
}
我的建议是使用格式为 yyyy-MM-dd
的日期字符串作为字典键而不是日期组件。这个字符串可以排序。
let groupedList = Dictionary(grouping: rawTransactionsList, by: { list -> String in
let dateObj = list.transactionTime.convertToDateObj()
let comps = Calendar.current.dateComponents([.day, .month, .year], from: dateObj)
return String(format: "%ld-%.2ld-%.2ld", comps.year!, comps.month!, comps.day!)
})
groupedtransactionsList = groupedList.keys.sorted(by: >).map { groupedList[[=10=]]! }