thieumao

Sort array String in Swift

Mar 8th, 2017
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 0.58 KB | None | 0 0
  1. import UIKit
  2.  
  3. class ViewController: UIViewController {
  4.  
  5.     override func viewDidLoad() {
  6.         super.viewDidLoad()
  7.         let persons = ["ab", "bb", "ab", "ba", "aa"]
  8.         let sortedPersons = persons.sorted { (s1: String, s2: String) -> Bool in
  9.             return s1 < s2
  10.         }
  11.         let sortedPersons2 = persons.sorted(by: mySorted)
  12.         print("persons", persons)
  13.         print("sortedPersons", sortedPersons)
  14.         print("sortedPersons2", sortedPersons2)
  15.     }
  16.  
  17.     func mySorted(s1: String, s2: String) -> Bool {
  18.         return s1 < s2
  19.     }
  20.    
  21. }
Advertisement
Add Comment
Please, Sign In to add comment