Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sorting of array of dictionary with date Swift 3

I have an array called myArray in which dictionaries are added I want that dictionary to be sorted by time which is a key in dictionary. And that time is in String. The date format of time is "yyyy/MM/dd HH:mm:ss"

I tried with below code solution but gives a warning of "Cast from 'String?' to unrelated type 'Date' always fails".

let sortedArray = self.myArray.sorted{ ($0["Time"] as? Date)! > ($1["Time"] as? Date)! }
print(sortedArray)

If anyone can help me out, Thank You.

like image 595
shahin ali agharia Avatar asked Dec 14 '22 23:12

shahin ali agharia


1 Answers

You don't need to convert to date time for this sort. The international format (yyyy/MM/dd HH:mm:ss) you're using provides the right sorting order as a string.

like image 84
Alain T. Avatar answered Dec 24 '22 16:12

Alain T.