Sort the array of objects in ascending order


This question already has an answer here:

  • Swift how to sort array of custom objects by property value 12 answers

I have an array of objects. Each object has a key "username".

Is there a swift way of sorting this array of objects into ascending order?

(array(at: indexPath.row) as AnyObject).value(forKey: "username") as? String)!


yourArray.sorted({ $0.username < $1.username})