按日期swift排序自定义对象数组

时间:2022-01-21 16:45:10

I have a custom object "Meeting" I am adding all the objects in an array. What I want to do is sort the array from Meeting.meetingDate.

我有一个自定义对象“会议”我正在添加数组中的所有对象。我想要做的是从Meeting.meetingDate排序数组。

I'm also using Parse, I have two queries and putting the results into one array.

我也在使用Parse,我有两个查询并将结果放入一个数组中。

I am able to add the data to "meetingData", but not sort it.

我能够将数据添加到“meetingData”,但不能对其进行排序。

Can anyone help with this?

有人能帮忙吗?

So my array is:

所以我的阵列是:

var meetingsData = [Meeting]()

The Meeting class is:

会议班是:

public class Meeting: PFObject, PFSubclassing
{

  @NSManaged public var meetingTitle: String?
  @NSManaged public var meetingLocation: String?
  @NSManaged public var meetingNotes: String?
  @NSManaged public var meetingDuration: String?
  @NSManaged public var createdBy: User?
  @NSManaged public var meetingTime: NSDate?
  @NSManaged public var meetingDate: NSDate?

}

The contents of the array is like so:

数组的内容如下:

<Meeting: 0x125edd040, objectId: xIqx7MoRd6, localId: (null)> {
createdBy = "<PFUser: 0x125d627c0, objectId: RKCWJRj84O>";
meetingDate = "2015-08-29 17:07:12 +0000";
meetingDuration = "2 hours 2 mins";
meetingLocation = 4th Floor;
meetingNotes = With Investors;
meetingTime = "2015-08-24 09:00:17 +0000";
meetingTitle = Meeting with Investors;
}
<Meeting: 0x125ed95f0, objectId: tz4xx5p9jB, localId: (null)> {
createdBy = "<PFUser: 0x125d627c0, objectId: RKCWJRj84O>";
meetingDate = "2015-08-23 23:00:00 +0000";
meetingDuration = "1 hour";
meetingLocation = "Emirates Stadium, London";
meetingNotes = "Football";
meetingTime = "2000-01-01 20:00:00 +0000";
meetingTitle = "Liverpool V Arsenal";
}
<Meeting: 0x125ed95f0, objectId: tz4xx5p9jB, localId: (null)> {
createdBy = "<PFUser: 0x125d627c0, objectId: RKCWJRj84O>";
meetingDate = "2015-09-23 23:00:00 +0000";
meetingDuration = "1 hour";
meetingLocation = "Library";
meetingNotes = "Dev Team";
meetingTime = "2000-01-01 10:00:00 +0000";
meetingTitle = "Code Review";
}

I have tried to do this Swift how to sort array of custom objects by property value but it doesn't work

我试图这样做Swift如何按属性值排序自定义对象数组但它不起作用

Thanks in advance

提前致谢

3 个解决方案

#1


0  

For Swift 4

对于Swift 4

Sorting by timestamp

按时间戳排序

values.sorted(by: { $0.meetingDataTimestamp > $1. meetingDataTimestamp})

#2


46  

NSDate can't be compared with < directly. You can use NSDate's compare method, like in the following code:

NSDate无法与 直接比较。您可以使用nsdate的比较方法,如下面的代码所示:

meetingsData.sort({ $0.meetingDate.compare($1.meetingDate) == .OrderedAscending })

In the above code you can change the order of the sort using the NSComparisonResult enum, that in Swift is implemented like the following:

在上面的代码中,您可以使用NSComparisonResult枚举更改排序顺序,在Swift中实现如下所示:

enum NSComparisonResult : Int {
   case OrderedAscending
   case OrderedSame
   case OrderedDescending
}

I hope this help you.

我希望这对你有帮助。

#3


17  

For Swift 3

对于Swift 3

meetingsData.sort(by: { $0.meetingDate.compare($1.meetingDate) == .orderedAscending})

OR

要么

As dates are now directly comparable in Swift 3, For example.

例如,日期现在可以在Swift 3中直接比较。

   let firstDate = Date()
   let secondDate = Date()

   if firstDate < secondDate {    
   } else if firstDate == secondDate {        
   } else if firstDate > secondDate {        
   }

#1


0  

For Swift 4

对于Swift 4

Sorting by timestamp

按时间戳排序

values.sorted(by: { $0.meetingDataTimestamp > $1. meetingDataTimestamp})

#2


46  

NSDate can't be compared with < directly. You can use NSDate's compare method, like in the following code:

NSDate无法与 直接比较。您可以使用nsdate的比较方法,如下面的代码所示:

meetingsData.sort({ $0.meetingDate.compare($1.meetingDate) == .OrderedAscending })

In the above code you can change the order of the sort using the NSComparisonResult enum, that in Swift is implemented like the following:

在上面的代码中,您可以使用NSComparisonResult枚举更改排序顺序,在Swift中实现如下所示:

enum NSComparisonResult : Int {
   case OrderedAscending
   case OrderedSame
   case OrderedDescending
}

I hope this help you.

我希望这对你有帮助。

#3


17  

For Swift 3

对于Swift 3

meetingsData.sort(by: { $0.meetingDate.compare($1.meetingDate) == .orderedAscending})

OR

要么

As dates are now directly comparable in Swift 3, For example.

例如,日期现在可以在Swift 3中直接比较。

   let firstDate = Date()
   let secondDate = Date()

   if firstDate < secondDate {    
   } else if firstDate == secondDate {        
   } else if firstDate > secondDate {        
   }