Reputation: 9830
I have an array
of structs
. In the struct
I have two NSDate
objects: prop1
and prop2
. I'm trying to sort prop1
from newest to oldest date/time. And I want prop2
to also get ordered based on prop1
. (I will also want to do vice versa.)
struct Item {
let prop1 : NSDate
let prop2 : NSDate
}
var myItem = [Item]()
myItem.insert(Item(prop1: myDateSecond, prop2: anotherDateSecond), atIndex: 0)
myItem.insert(Item(prop1: myDateThird, prop2: anotherDateThird), atIndex: 0)
myItem.insert(Item(prop1: myDateFirst, prop2: anotherDateFirst), atIndex: 0)
myItem.sort { $0.prop1 < $1.prop1 }
At the last line of code, I get the following error:
Cannot invoke 'sort' with an argument list of type '((_, _) -> _)'
What am I doing wrong, and how can I fix it?
Upvotes: 2
Views: 61
Reputation: 236260
When comparing two dates you have to use NSDate method compare:
struct Item {
let prop1 : NSDate
let prop2 : NSDate
}
var myItem = [Item]()
myItem.insert(Item(prop1: myDateSecond, prop2: anotherDateSecond), atIndex: 0)
myItem.insert(Item(prop1: myDateThird, prop2: anotherDateThird), atIndex: 0)
myItem.insert(Item(prop1: myDateFirst, prop2: anotherDateFirst), atIndex: 0)
myItem.sort{$0.prop1.compare($1.prop1) == NSComparisonResult.OrderedAscending}
Upvotes: 1