You have an array of dictionaries of the form [String: String]
and a dictionary does not have a name property, so
$0.name is invalid (in any Swift version).
Retrieving the dictionary value is done via subscripting,
e.g. $0["name"], which returns an optional.
In Swift 2 you could compare optionals directly (and nil was
considered "less" than any non-nil value). Therefore in Swift 2
you could sort the array with
arr.sortInPlace { $0["name"] < $1["name"] }
However, the optional comparison operators have been removed in
Swift 3 with the implementation of
SE-0121 – Remove Optional Comparison Operators.
Therefore you have to unwrap $0["name"].
If you are 100% sure that every dictionary in the array has a "name" key
then you can unwrap forcefully
as Ahmad F suggested
arr.sort { $0["name"]! < $1["name"]! }
If that is not guaranteed then provide a default name, e.g. the empty string via the nil-coalescing operator ??:
arr.sort { ($0["name"] ?? "") < ($1["name"] ?? "") }
Example:
var arr = [["title":"Mrs","name":"Abc"], ["title":"Mr","name":"XYZ"], ["title": "Dr"]]
arr.sort { ($0["name"] ?? "") < ($1["name"] ?? "") }
print(arr)
// [["title": "Dr"], ["name": "Abc", "title": "Mrs"], ["name": "XYZ", "title": "Mr"]]
As one can see, the dictionary without a "name" key is ordered first.
A simple way to sort entries without name last would be:
arr.sort { ($0["name"] ?? "\u{10FFFF}") < ($1["name"] ?? "\u{10FFFF}") }
arr.sorted { $0["name"]! < $1["name"]! }