Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays Swift-一次性按日期和时间对数组进行排序_Arrays_Swift_Sorting - Fatal编程技术网

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

Arrays Swift-一次性按日期和时间对数组进行排序,arrays,swift,sorting,Arrays,Swift,Sorting,我有一个数组,其字符串属性类似于日期(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-

我有一个数组,其字符串属性类似于日期(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实现这一点


非常感谢您抽出时间

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

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

首先,请以小写字母(
array
)开头命名变量

您可以通过连接字符串对数组进行简单排序,因为格式
yyyy-MM-dd HH:MM
是可排序的

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

这个答案是根据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 

通过不强制展开
日期?
,还有改进的余地,尽管字符串清理使之相当安全,并且通过使用类级静态日期格式化程序。但我将把这种改进留给实现:-)

我认为您可以将日期和时间转换为“Unix时间戳”,然后使用排序方法。请看,是的,对不起,我在代码中以小写字母命名变量。这只是一个虚构的例子。好的,我明白了,你说的很有道理,你是个和蔼可亲的人!!但我还有一个问题。让我更具体一点,我正在构建一个足球得分应用程序,我需要在进球发生时实时显示进球,我使用一个api,它为我提供了3个变量,比赛日期、比赛时间和得分时间。我该如何排序,以使我的最新进球排在榜首?这是另一个问题,信息可能更具体,但不够具体。
score\u time
包含哪些内容?另一个字符串或一个
Int
代表进球时相对于比赛时间的分钟数它是一个字符串,包含进球时的分钟数,如:“87”或“23”或“90+2”,这并不好。日期/时间的字符串排序是不稳定的,因为更改存储格式会悄悄地破坏排序(并且只针对某些数据)。此外,它比仅使用时间加法操作要慢,这可能会因元素的数量而受到影响。@Alexander实际上更改存储格式会破坏任何代码。