Sorting array gives me error

Jessica

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?

Leo Dabus

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}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Sorting array gives me error

From Dev

Create empty array of struct gives me an error

From Dev

Create empty array of struct gives me an error

From Dev

MySQL 'AND' gives me error

From Dev

It gives me an error at GPS

From Dev

Animate UITableViewCell gives me error

From Dev

php header gives me the error

From Dev

.getDownloadUrl() gives me error - Android

From Dev

using "this" in a fragment gives me an error

From Dev

Animate UITableViewCell gives me error

From Dev

Firefox gives me dbus error

From Dev

php sorting array gives: Undefined offset: 6

From Dev

Javascript Array Sorting Error

From Dev

logical error in sorting of array

From Dev

Array Sorting getting error

From Dev

Storing an array/matrix of ClassificationSVM with fitcsvm() function in Matlab gives me an error, how can I resolve it?

From Dev

Addition multiple array of extra attributes in eloquent many-to-many insertion gives me error

From Dev

same sorting algorithm,same array,but gives different time for sorting when sorting same array few times

From Dev

String in Array of numbers gives error

From Dev

sizeof() gives an error for array argument

From Dev

LocalDate in array gives me null result

From Dev

Pip freeze gives me this error related with git

From Dev

Change UITableViewCell size gives me an error

From Dev

Devise confirmation link gives me routing error

From Dev

<~ in elm 0.16.0 gives me a compile time error

From Dev

Rails Generate Controller gives me load error

From Dev

Wildfly Resteasy gives me 404 error and no logging

From Dev

Why triggerHandler(event) gives me an error?

From Dev

Istanbul gives me coverage but ends output with an error

Related Related

HotTag

Archive