ios

closure - sort

boywin1992 2022. 5. 24. 20:03
728x90

// closure - sort

import UIKit

 

var sports = ["foot ball", "tenis", "table tenis", "wrestle", "base ball"]

 

sports.sort { $0 < $1 }

print(sports)

// result : ["base ball", "foot ball", "table tenis", "tenis", "wrestle"]

 

sports.sort { $0 > $1 }

print(sports)

// result : ["wrestle", "tenis", "table tenis", "foot ball", "base ball"]

 

sports.sort (by : { $0 < $1 })

print(sports)

// result : ["base ball", "foot ball", "table tenis", "tenis", "wrestle"]

 

sports.sort (by : { $0 > $1 })

print(sports)

// result : ["wrestle", "tenis", "table tenis", "foot ball", "base ball"]

 

sports.sort (by : < )

print(sports)

// result : ["base ball", "foot ball", "table tenis", "tenis", "wrestle"]

 

sports.sort (by : > )

print(sports)

// result : ["wrestle", "tenis", "table tenis", "foot ball", "base ball"]

 

sports.sort { (a, b) -> Bool in

    return a < b

}

print(sports)

// result : ["base ball", "foot ball", "table tenis", "tenis", "wrestle"]

 

sports.sort { (a, b) -> Bool in

    return a > b

}

print(sports)

// result : ["wrestle", "tenis", "table tenis", "foot ball", "base ball"]

728x90