Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort array of custom objects by date swift

Tags:

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.

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

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

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

Thanks in advance

like image 740
Hassan Mahmood Avatar asked Aug 25 '15 13:08

Hassan Mahmood


People also ask

How do I sort an array by Date in Swift?

Map your array of history objects to an array of Date objects using a DateFormatter. Use the zip() function to combine the array of history objects and the array of date objects into an array of tuples. Sort the array of tuples. Map the array of tuples back to an array of history objects.

How do I sort a custom array in Swift?

To use it, call sort() over a mutable array value. <1> Since sort() is mutating function, you have to declare array as var . Otherwise, you will get the following error. sort() will use the less-than operator ( < ) when making a comparison among elements, which result in ascending sort order.

How do I sort a string by Date in Swift?

To properly sort it you first need to convert your tSFDATE string to Date and then sort it. let dateFormatter = DateFormatter() dateFormatter. dateFormat = "dd/MM/yyyy - HH:mm:ss" let sortedArray = trackingListArr. sorted { dateFormatter.


2 Answers

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

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:

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

I hope this help you.

like image 147
Victor Sigler Avatar answered Dec 19 '22 19:12

Victor Sigler


For Swift 3

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

OR

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

   let firstDate = Date()    let secondDate = Date()     if firstDate < secondDate {        } else if firstDate == secondDate {            } else if firstDate > secondDate {            } 
like image 36
Ramkrishna Sharma Avatar answered Dec 19 '22 17:12

Ramkrishna Sharma