Gavin Ray
03/12/2020, 5:10 PMval sortedGames = games.sortedWith(compareBy{
Instant.now().compareTo(it.dateTime)
})
Evan R.
03/12/2020, 5:13 PMval now = Instant.now()
val sortedGames = games.sortedBy { now > it.dateTime }
Evan R.
03/12/2020, 5:13 PM>
and <
operators just use .compareTo()
😉Gavin Ray
03/12/2020, 5:16 PMGavin Ray
03/12/2020, 5:16 PMGavin Ray
03/12/2020, 5:17 PMimport java.util.*
import java.time.Instant
val gameTimes = listOf(
"2020-03-09T08:00:00.000Z",
"2020-03-09T09:00:00.000Z",
"2020-03-09T10:00:00.000Z",
"2020-03-09T11:00:00.000Z",
"2020-03-09T12:00:00.000Z",
"2020-03-09T13:00:00.000Z",
"2020-03-09T14:00:00.000Z"
).map { Instant.parse(it) }
val currentTime = Instant.parse("2020-03-09T10:00:00.000Z")
val sortedGames1 = gameTimes.sortedWith(compareBy{
currentTime.compareTo(it)
})
val sortedGames2 = gameTimes.sortedBy {
currentTime > it
}
println(sortedGames1)
println(sortedGames2)
Gavin Ray
03/12/2020, 5:21 PMsortedGames1: [2020-03-09T11:00:00Z, 2020-03-09T12:00:00Z, 2020-03-09T13:00:00Z, 2020-03-09T14:00:00Z, 2020-03-09T10:00:00Z, 2020-03-09T08:00:00Z, 2020-03-09T09:00:00Z]
sortedGames2:
[2020-03-09T10:00:00Z, 2020-03-09T11:00:00Z, 2020-03-09T12:00:00Z, 2020-03-09T13:00:00Z, 2020-03-09T14:00:00Z, 2020-03-09T08:00:00Z, 2020-03-09T09:00:00Z]
Jakub Pi
03/12/2020, 6:02 PMval sortedGames2 = gameTimes.sortedBy(currentTime::compareTo)
The .sortedBy
is just syntactic sugar for .sortedWith(compareBy...
Gavin Ray
03/12/2020, 6:39 PMGavin Ray
03/12/2020, 6:39 PMGavin Ray
03/12/2020, 6:42 PMJakub Pi
03/12/2020, 6:48 PMGavin Ray
03/12/2020, 6:59 PMGavin Ray
03/12/2020, 7:01 PMpartitionBy
to separate the list into two chunks for which events are past/upcoming. I am not sure if Kotlin has a partition function.Gavin Ray
03/12/2020, 7:02 PMGavin Ray
03/12/2020, 7:07 PMval (upcoming, past) = games.partition {
it.dateTime.isAfter(currentTime)
}
Jakub Pi
03/12/2020, 8:02 PMJakub Pi
03/12/2020, 8:29 PMval sortedGames = gameTimes
.groupBy{ it.compareTo(currentTime).sign}
.mapValues{ (k, v) -> v.sorted() }
println (listOf(1,0,-1)
.flatMap{sortedGames
.getOrElse(it, {emptyList()})})
Gavin Ray
03/12/2020, 8:32 PMgroupBy
was the way the go about 15 mins ago but was having trouble figuring out how to do the actual grouping codeGavin Ray
03/12/2020, 8:32 PMJakub Pi
03/12/2020, 8:34 PMGavin Ray
03/12/2020, 8:39 PMGavin Ray
03/12/2020, 8:40 PMval (past, happening, upcoming) = gameTimes.groupBy {
currentTime.compareTo(it)
}.values
Gavin Ray
03/12/2020, 8:40 PMerror: destructuring declaration initializer of type Collection<List<Instant!>> must have a 'component1()' function
Gavin Ray
03/12/2020, 8:47 PMwhatever.component1()
, etc under the hood. List type implements these functions, Collection does not. So I just needed to convert it to List:Gavin Ray
03/12/2020, 8:48 PMval (past, happening, upcoming) = gameTimes.groupBy {
currentTime.compareTo(it)
}.values.toList()
Gavin Ray
03/12/2020, 8:48 PMJakub Pi
03/12/2020, 8:56 PMJakub Pi
03/12/2020, 8:56 PMGavin Ray
03/12/2020, 8:59 PMYou can't guarantee the ordering of the map returned by groupBy, not necessarily sortedWhat does this mean? When I did
println
on it, it was -1 -> (past), 0 -> (happening). 1-> (upcoming)
, but you are saying that there are cases in which this could change?Gavin Ray
03/12/2020, 9:00 PM.toList()
and listOf()
besides personal style preference?Jakub Pi
03/12/2020, 9:01 PMJakub Pi
03/12/2020, 9:02 PMGavin Ray
03/12/2020, 9:05 PMJakub Pi
03/12/2020, 9:05 PMGavin Ray
03/12/2020, 10:23 PMGavin Ray
03/12/2020, 10:24 PM