1. compareTo

2. compareTo의 동작 규칙

  1. 비대칭적 동작

    if (a > b) {
        b < a
    }
    
  2. 연속적 동작

    if (a > b && b > c) {
        a > c
    }
    
  3. 코넥스적 동작

    a >= b || b >= a // 항상 true
    

3. compareTo를 따로 정의해야할까?

// sortedBy를 사용하면 원하는 키로 정렬 가능
val sorted = names.sortedBy { it.surname }

// sortedWith으로 여러 프로퍼티 기반 정렬도 가능
val sorted = names.sortedWith(compareBy({ it.surname }, { it.name }))

4. comapareTo 구현하기

  class User (
    val name: String,
    val surname: String
  ) : Comparable<User> {
    override fun compareTo(other: User): Int =
      compareValues(surname, other.surname)
  }