Swift -一次性按日期和时间对数组排序

k97glaaz  于 2023-05-27  发布在  Swift
关注(0)|答案(3)|浏览(125)

我有一个数组,它的字符串属性类似于日期(yyyy-MM-dd),另一个字符串属性类似于时间(HH:mm)。
我正在尝试按日期排序数组,然后在1次扫描中的时间。
示例:

Array[0].date = 2019-11-18
Array[0].time = 19:00
Array[1].date = 2019-11-18
Array[1].time = 22:00
Array[2].date = 2019-10-14
Array[2].time = 16:00
Array[3].date = 2019-11-16
Array[3].time = 13:00
Array[4].date = 2019-11-16
Array[4].time = 14:00

我想实现

Array[0].date = 2019-11-18
Array[0].time = 22:00
Array[1].date = 2019-11-18
Array[1].time = 19:00
Array[2].date = 2019-10-16
Array[2].time = 14:00
Array[3].date = 2019-10-16
Array[3].time = 13:00
Array[4].date = 2019-11-14
Array[4].time = 16:00.

如何使用Swift实现这一点?
非常感谢您的宝贵时间!

qc6wkl3g

qc6wkl3g1#

这个答案是对OP下面评论中的问题的改进,以回应@vadian的答案。实际的需求是对API提供的足球进球时间进行排序。下面的解决方案为这个数据创建了一个结构体,其中包含一个实际目标时间的计算变量,然后按此进行排序。

struct Goal{
   let matchDate: String
   let matchTime: String
   let goalTime: String

   var timeOfGoal: Date {
      let goalComponents = goalTime.components(separatedBy: "+").map{$0.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines.union(CharacterSet.decimalDigits.inverted))}
      let goalSeconds = TimeInterval(60 * goalComponents.compactMap({Int($0)}).reduce(0, +))
      let dateFormatter = DateFormatter()
      dateFormatter.dateFormat = "yyyy-MM-dd HH:mm"
      let startTime = dateFormatter.date(from: matchDate + " " + matchTime)!
      return startTime.addingTimeInterval(goalSeconds)
   }
}

我做了如下测试

let goals = [
   Goal(matchDate: "2019-11-18", matchTime: "22:00", goalTime: "90 +7"),
   Goal(matchDate: "2019-11-18", matchTime: "19:00", goalTime: "22"),
   Goal(matchDate: "2019-11-18", matchTime: "22:00", goalTime: "99"),
   Goal(matchDate: "2019-11-18", matchTime: "19:00", goalTime: "45 + 3"),
   Goal(matchDate: "2019-11-18", matchTime: "19:00", goalTime: "45+6"),
   Goal(matchDate: "2019-11-18", matchTime: "22:00", goalTime: "90+6"),
   Goal(matchDate: "2019-11-18", matchTime: "22:00", goalTime: "35"),
   Goal(matchDate: "2019-11-18", matchTime: "22:00", goalTime: "85"),
   Goal(matchDate: "2019-11-18", matchTime: "22:00", goalTime: "90"),
   Goal(matchDate: "2019-11-18", matchTime: "22:00", goalTime: "90+ 8"),
   Goal(matchDate: "2019-11-18", matchTime: "19:00", goalTime: "44")]

let ordered = goals.sorted{$0.timeOfGoal > $1.timeOfGoal}

ordered.forEach{print("\($0.matchDate) - \($0.matchTime) - \($0.goalTime) ")}

它正确地产生了:

2019-11-18 - 22:00 - 99 
2019-11-18 - 22:00 - 90+ 8 
2019-11-18 - 22:00 - 90 +7 
2019-11-18 - 22:00 - 90+6 
2019-11-18 - 22:00 - 90 
2019-11-18 - 22:00 - 85 
2019-11-18 - 22:00 - 35 
2019-11-18 - 19:00 - 45+6 
2019-11-18 - 19:00 - 45 + 3 
2019-11-18 - 19:00 - 44 
2019-11-18 - 19:00 - 22

通过不强制解包Date?(尽管字符串清理使其相当安全)和使用类级别的静态DateFormatter,还有改进的余地。但我将把这种细化留给实现:-)

vptzau2j

vptzau2j2#

首先,请以小写字母(array)开始命名变量
您可以简单地通过连接字符串来对数组进行排序,因为格式yyyy-MM-dd HH:mm是可排序的。

array.sort{"\($0.date) \($0.time)" > "\($1.date) \($1.time)"}
qv7cva1a

qv7cva1a3#

您可以附加日期和时间字符串,因为最大的时间间隔(年)在左侧,最小的时间间隔(分钟)在右侧。按标准词典编纂方法排序将把“最大”的日期/时间组合放在第一位。

let sortedArray = myArray.sorted(by: { ($0.date + $0.time) > ($1.date + $1.time) })

相关问题