-
Notifications
You must be signed in to change notification settings - Fork 23
/
Contents.swift
43 lines (31 loc) · 1 KB
/
Contents.swift
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
39
40
41
42
43
import UIKit
// Exercise - Adopt Protocols: CustomStringConvertible, Equatable, and Comparable
class Human: CustomStringConvertible, Equatable, Comparable {
var name: String
var age: Int
init(name: String, age: Int) {
self.name = name
self.age = age
}
var description: String {
return "\(name), \(age)"
}
static func < (lhs: Human, rhs: Human) -> Bool {
return lhs.age < rhs.age
}
static func == (lhs: Human, rhs: Human) -> Bool {
return lhs.name == rhs.name && lhs.age == rhs.age
}
}
var person1 = Human(name: "Adam", age: 26)
var person2 = Human(name: "Joanne", age: 27)
print(person1)
print(person2)
print(person1 == person2)
print(person1 != person2)
var person3 = Human(name: "John", age: 33)
var person4 = Human(name: "Emily", age: 42)
var person5 = Human(name: "James", age: 23)
var people: [Human] = [person1, person2, person3, person4, person5]
var sortedPeople: [Human] = people.sorted()
print(sortedPeople)