let names = ["Chris", "Alex", "Ewa", "Barry", "Daniella"]
func backwards(s1: String, s2: String) -> Bool {
return s1 > s2
}
var reversed = names.sort(backwards)
print(reversed)
var ascending = names.sort({ (s1: String, s2: String) -> Bool in
return s1 < s2
})
print(ascending)
let sortAscending = { (s1: String, s2: String) -> Bool in
return s1 < s2
}
ascending = names.sort(sortAscending)
I am suppose to sort this code according to the number of characters they have from the most to the least. For example, Daniella has 8 characters so she will be fist in the list. The output I am suppose to get is
["Daniella", "Barry", "Chris", "Alex", "Ewa"]
sortvssorteddiscussion below — I don't think your code compiles at all :(sorted, right?sortand expecting a return value, makes no sense, even when in-place sorting mutable collections. BTW, does Swift returns an empty tuple (aka,Void) in such scenarios? That would explain why his code compiles...