Question: I'm working on an iOS project in swift 4.
I have an array of dictionarylike

```
var myArray = [[String: AnyObject]]()

```

The dictionary contains, name, age created\_at(date in string), nationality etc..
And I need to sort the myArray with the ascending order of 'created\_at' key which is string format of date. How can I do that? Please help me. I checked all solutions, but i didnt get the sorted array.

my array is like,

```
(
    {
        name: X
        age: 26
        created_at: "1991-10-10 18:29:05"
        nationality: Indian
    },
    {
        name: Y
        age: 30
        created_at: "2000-05-10 18:29:05"
        nationality: Indian
    },
)

```

Answer: 
I would suggest to create a model for these but of course you can do it like this as well

```
array.sort { (firstItem, secondItem) -> Bool in
    let dateFormatter = DateFormatter()
    dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"

    if let dateAString = firstItem["created_at"] as? String,
        let dateBString = secondItem["created_at"] as? String,
        let dateA = dateFormatter.date(from: dateAString),
        let dateB = dateFormatter.date(from: dateBString){
        return dateA.compare(dateB) == .orderedAscending
    }
    return false
}

```
In Your case the key `created_at: "1991-10-10 18:29:05"` is your date string so you have to convert your date string into a `Date` object and get the time interval using `Date().timeIntervalSince1970` method. It will return a `Double` and you can compare your date using this `Double` value.

Please refer to the below code snippet for more convenience. 

```
//Model For your JSON
struct MyModel:Codable {
    var age:Int?
    var name:String?
    var nationality:String?
    var created_at:String?
}

//MY Array
var dataSource = [MyModel]()

//Helper method to convert the date string into timeInterval
func getMilisecFrom(dateString:String) -> Double{
    let formator = DateFormatter()
    formator.dateFormat = "yyyy-MM-dd HH:mm:ss"
    guard let date = formator.date(from: dateString)
        else {
            return Date().timeIntervalSince1970
    }
    return date.timeIntervalSince1970
}

// method to short the array
func ShortByDate(){
    let shortedData = dataSource.sorted(by: {(getMilisecFrom(dateString: $0.created_at ?? "")) > (getMilisecFrom(dateString: $1.created_at ?? ""))})
    dataSource = shortedData
}

```