티스토리 뷰

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

'ios' 카테고리의 다른 글

enum : Character & String  (0) 2022.05.24
short cut  (0) 2022.05.24
enum - case, case, case  (0) 2022.05.24
enum : Int  (0) 2022.05.24
main interface (start view)  (0) 2022.05.24
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/07   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
글 보관함