gpt4 book ai didi

arrays - 按日期快速排序自定义对象数组

转载 作者:IT王子 更新时间:2023-10-29 05:12:42 24 4
gpt4 key购买 nike

我有一个自定义对象“ session ”,我将所有对象添加到一个数组中。我想要做的是对 Meeting.meetingDate 中的数组进行排序。

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

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

有人可以帮忙吗?

所以我的数组是:

var meetingsData = [Meeting]()

session 类是:

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?

}

数组的内容是这样的:

<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";
}

我试过这样做Swift how to sort array of custom objects by property value但它不起作用

提前致谢

最佳答案

NSDate无法与<相提并论直接地。您可以使用 NSDate compare 方法,如以下代码:

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

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

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

希望对你有帮助

关于arrays - 按日期快速排序自定义对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32205919/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com