The most efficient way to sort in SWIFT
Using Operator overloading is the most efficient way to sort strings in Swift.
// OPERATOR OVERLOADING let names = ["Chris", "Alex", "Ewa", "Barry", "Daniella"] var sortedNames = sorted(names, <) var reverseOrder = sorted(names, >)
In the above > and < commands, the operators are overloaded in Swift to sort the lines.
I tested the code on the Playground and came to the conclusion that when using operator overloading, it is best to sort the lines.
Copy below on the Playground.
let names = ["Chris", "Alex", "Ewa", "Barry", "Daniella"] var reversed = sorted (names, // This is a closure { (s1 : String, s2 : String) -> Bool in return s1 > s2 } ) println(reversed) var reverseOrder = sorted(names, {s1, s2 in s1 > s2}) var reverseOrder2 = sorted(names, { $0 > $1} ) // OPERATOR OVERLOADING var reverseOrder3 = sorted(names, >)
Conclusion from the playground:

From the above image, you can see that all other methods should list the loops to sort 5 lines. Where, as when using Operator overload, it is not necessary to enumerate a loop to sort the lines.
Link from Swift documentation
Kampai Jan 22 '15 at 10:27 2015-01-22 10:27
source share