Shumilin Alexandr
04/30/2022, 8:43 PMdata class MyDate(val year: Int, val month: Int, val dayOfMonth: Int) : Comparable<MyDate> {}
function:
fun test(date1: MyDate, date2: MyDate) {
// this code should compile:
println(date1 < date2)
}
Problem: I need to write a method for comparing MyDate objects
I write a solution, it works ok
override fun compareTo(other: MyDate): Int {
val yearDiff = year - other.year
val monthDiff = month - other.month
val dayDiff = dayOfMonth - other.dayOfMonth
if (yearDiff == 0) {
if (monthDiff == 0) {
if (dayDiff == 0) {
return 0
}
} else return monthDiff
} else return yearDiff
return 0;
}
and now “good” solution:
override fun compareTo(other: MyDate) = when {
year != other.year -> year - other.year
month != other.month -> month - other.month
else -> dayOfMonth - other.dayOfMonth
}
i am not understand how works “good” solution? can you help me?
what does mean
year != other.year -> year - other.year
Chris Lee
04/30/2022, 9:14 PMephemient
05/01/2022, 1:07 AMoverride fun compareTo(other: MyDate): Int =
compareValuesBy(this, other, { it.year }, { it.month }, { it.dayOfMonth })
which is not only simpler, but also avoids overflow problems with -
val a = MyDate(0, 0, 0)
val b = MyDate(Int.MIN_VALUE, 0, 0)
you would expect that a > b
, but due to -
, your comparison will actually produce a < b
Matteo Mirk
05/02/2022, 3:19 PMephemient
05/03/2022, 2:15 AM-
isn't a good solution either way, IMO. if you don't want to use the stdlib functions, then at least
when {
year < other.year -> -1
year > other.year -> 1
month < other.month -> -1
month > other.month -> 1
dayOfMonth < other.dayOfMonth -> -1
dayOfMonth > other.dayOfMonth -> 1
else -> 0
}
avoids overflow issuesnkiesel
05/03/2022, 4:59 AMyear
and month
are equal but dayOfMonth
is notShumilin Alexandr
05/04/2022, 7:39 AMcompareValuesBy(this, other, { it.year }, { it.month }, { it.dayOfMonth })
imhonkiesel
05/04/2022, 8:33 AMcompareValuesBy
is the best solution. but your "ok" is not ok: it will return 0
instead of 5
for MyDate(2022, 1, 10).compareTo(MyDate(2022, 1, 5))
. You have to use
if (yearDiff == 0) {
if (monthDiff == 0) {
return dayDiff
} else return monthDiff
} else return yearDiff
Shumilin Alexandr
05/04/2022, 8:38 AMoverride fun compareTo(other: MyDate): Int {
val yearDiff = year - other.year
val monthDiff = month - other.month
val dayDiff = dayOfMonth - other.dayOfMonth
if (yearDiff == 0) {
if (monthDiff == 0) {
if (dayDiff == 0) {
return 0
} else return dayDiff
} else return monthDiff
} else return yearDiff
}
and now correctephemient
05/05/2022, 5:52 PM-
. if you really wanted to keep that structure for some reason,
val yearComparison = compareValues(year, other.year)
val monthComparison = compareValues(month, other.month)
val dayComparison = compareValues(day, other.day)
return when {
yearComparison != 0 -> yearComparison
monthComparison != 0 -> monthComparison
else -> dayComparison
}