如何对数组中的日期进行分组?
1:假设您有以下数据
2:使日期组```let dictGroupDate = Dictionary(grouping: arrDates) { (strdate) -> String in let df = DateFormatter() df.dateFormat = 'yyyy-MM-dd'T'HH:mm:ss.SSSZZZZ' let date = df.date(from: strdate) df.dateFormat = 'yyyy-MM-dd' return df.string(from: date!) }
3:获取所有节日期并对它们进行排序```let arrDateKey = Array(dictGroupDate.keys).sorted { (strdate1, strdate2) -> Bool in
let df = DateFormatter()
df.dateFormat = 'yyyy-MM-dd'
let date1 = df.date(from: strdate1)
let date2 = df.date(from: strdate2)
return date1!.compare(date2!) == .orderedAscending
}
4:为每个日期排序时间并创建新字典``` var newGroupedDates = String : [String]
for strKey in arrDateKey { let sortedTime = dictGroupDate[strKey]?.sorted(by: { (strdate1, strdate2) -> Bool in let df = DateFormatter() df.dateFormat = 'yyyy-MM-dd'T'HH:mm:ss.SSSZZZZ' let date1 = df.date(from: strdate1) let date2 = df.date(from: strdate2)
return date1!.compare(date2!) == .orderedAscending
})
newGroupedDates[strKey] = sortedTime! }
5:显示数据 UITABLEVIEW```// NumberOfSections
arrDateKey.count
//NumberOfRows
let key = arrDateKey[indexPath.section]
newGroupedDates[key]?.count
// CellForRow
// To get correct date and Time
let key = arrDateKey[indexPath.section]
let item = newGroupedDates[key]![indexPath.row]
// Now item is the date string, and do whatever you wanted to do.
我正在迅速进行中,希望您能在obj-c中进行管理
假设您有以下数据
let arrDates = [ '2019-12-03T05:58:11.317Z ', '2019-12-03T05:58:10.317Z ', '2019-12-03T05:58:01.317Z ', '2019-12-03T05:55:12.448Z ', '2019-12-03T05:48:11.317Z ', '2019-12-03T05:28:11.317Z ', '2019-12-03T05:11:24.004Z ', '2019-12-03T05:28:11.317Z ', '2019-12-03T05:55:12.965Z ', '2019-12-02T15:09:35.408Z ', '2019-12-02T15:09:38.187Z ', '2019-12-02T15:43:02.118Z ', '2019-12-02T15:44:09.344Z ', '2019-12-02T17:07:55.038Z ', '2019-12-02T16:42:16.649Z ', '2019-12-01T16:42:16.649Z '] 使日期组
let dictGroupDate = Dictionary(grouping: arrDates) { (strdate) -> String in let df = DateFormatter() df.dateFormat = 'yyyy-MM-dd'T'HH:mm:ss.SSSZZZZ' let date = df.date(from: strdate) df.dateFormat = 'yyyy-MM-dd' return df.string(from: date!) } 获取所有节日期并对它们进行排序
let arrDateKey = Array(dictGroupDate.keys).sorted { (strdate1, strdate2) -> Bool in let df = DateFormatter() df.dateFormat = 'yyyy-MM-dd' let date1 = df.date(from: strdate1) let date2 = df.date(from: strdate2)
return date1!.compare(date2!) == .orderedAscending } 为每个日期排序时间并创建新字典
var newGroupedDates = String : [String]
for strKey in arrDateKey { let sortedTime = dictGroupDate[strKey]?.sorted(by: { (strdate1, strdate2) -> Bool in let df = DateFormatter() df.dateFormat = 'yyyy-MM-dd'T'HH:mm:ss.SSSZZZZ' let date1 = df.date(from: strdate1) let date2 = df.date(from: strdate2)
return date1!.compare(date2!) == .orderedAscending
})
newGroupedDates[strKey] = sortedTime! } 显示数据 UITABLEVIEW
// NumberOfSections arrDateKey.count
//NumberOfRows let key = arrDateKey[indexPath.section] newGroupedDates[key]?.count
// CellForRow // To get correct date and Time
let key = arrDateKey[indexPath.section] let item = newGroupedDates[key]![indexPath.row] // Now item is the date string, and do whatever you wanted to do. 输出量
最后的newGroupedDate```['2019-12-03': ['2019-12-03T05:11:24.004Z ', '2019-12-03T05:28:11.317Z ', '2019-12-03T05:28:11.317Z ', '2019-12-03T05:48:11.317Z ', '2019-12-03T05:55:12.448Z ', '2019-12-03T05:55:12.965Z ', '2019-12-03T05:58:01.317Z ', '2019-12-03T05:58:10.317Z ', '2019-12-03T05:58:11.317Z '], '2019-12-02': ['2019-12-02T15:09:35.408Z ', '2019-12-02T15:09:38.187Z ', '2019-12-02T15:43:02.118Z ', '2019-12-02T15:44:09.344Z ', '2019-12-02T16:42:16.649Z ', '2019-12-02T17:07:55.038Z '], '2019-12-01': ['2019-12-01T16:42:16.649Z ']]
赞0
踩0