Search code examples
arraysswiftsortingdatensdate

Sort Array of NSDates swift


I have an array of NSDates in swift. The dates are only hours and minutes. I have tried

times.sortInPlace({ $0.date.compare($1.date) == NSComparisonResult.OrderedAscending })

but I get an error "Static member 'date' cannot be used on instance of type 'NSDate' and I get the same error with the 1970 attempt as well.


Solution

  • edit/update:

    Swift 3 or later NSDate is now Date and it conforms to Comparable protocol. Mutating method sortInPlace has also been renamed to sort:

    times.sort()
    

    You are trying to access NSDate date property. Try like this:

    times.sortInPlace({ $0.compare($1) == NSComparisonResult.OrderedAscending })