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
}

```
You use the sort function of apple. We just only need to compare the value of 
 `created_at` field. Because when we compare string rely on ASCII to compare. 

```
let result = myArray.sorted(by: { (object1, object2) in bool
    guard let temp1 =  object1["created_at"] as? String,
        let temp2 = object2["created_at"] as? String else { return false }
        return temp1 < temp2
})

```