티스토리 뷰

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
32
33
34
35
36
37
38
class User {
    var age:Int
    var level:Int
    var name:String
    
    init(age:Int,level:Int,name:String) {
        self.age = age
        self.level = level
        self.name = name
    }
}
 
var users = [User]()
 
users.append(User(age: 25, level: 10, name: "김씨"))
users.append(User(age: 20, level: 20, name: "이씨"))
users.append(User(age: 23, level: 30, name: "박씨"))
users.append(User(age: 26, level: 40, name: "호박씨"))
users.append(User(age: 22, level: 50, name: "비타민씨"))
 
print("소팅하기전")
for user in users {
    print("\(user.name) - level:\(user.level) ,age:\(user.age)")
}
 
print("\n\n나이 적은순")
users = users.sorted(by: {$0.age < $1.age}) //나이 적은순으로 소팅
 
for user in users {
    print("\(user.name) - level:\(user.level) ,age:\(user.age)")
}
 
print("\n\n레벨 높은순")
users = users.sorted(by: {$0.level > $1.level}) //레벨 높은순으로 소팅
 
for user in users {
    print("\(user.name) - level:\(user.level) ,age:\(user.age)")
}
cs


댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday