需要帮助在Swift中按时间戳对结构进行排序

尝试按时间戳对Swift结构进行排序,确实需要一些帮助。我的结构定义如下:

struct TripDetails {

  let tripDepartureTime: String
  var myLocations: [GeoPoint]
  var myDates: [Timestamp]

}

在变量TodaysTrip中,包含以下数据:

TripDetails(
tripDepartureTime: "2020-05-23 22:56:52 +0000", 

myLocations: [<FIRGeoPoint: (21.561333, -80.033536)>, 
<FIRGeoPoint: (22.561288, -80.023592)>, 
<FIRGeoPoint: (20.561265, -80.063439)>], 

myDates: [<FIRTimestamp: seconds=1590274922 nanoseconds=795317000>, 
<FIRTimestamp: seconds=1590274802 nanoseconds=683353000>, 
<FIRTimestamp: seconds=1590274682 nanoseconds=679319000>])

我想按myDates时间戳以降序对特定行程进行排序。已经有一段时间了,无法弄清楚我在做什么错。我在StackOverflow上看到过数篇有关此问题的文章,但在我看来,这些文章都不起作用。

评论
  • 寂寞好了
    寂寞好了 回复

    If I understood you correctly, you need to sort myDates inside TripDetails structure. Then this code will help:

    var tripDetails = ... // Creating/Getting trip details
    
    tripDetails.myDates.sort(by: { $0.seconds > $1.seconds })