I have an object with a field that is distance, it can be in meters or kilo-meters, what I want to do is, sort that list first by meters and then by kilo-meters, so the meters are the first one in the list and then sort the kilo-meters. I used to do this with a Comparator but I don't know how to estructure this case.
What I used to do is a class that returns a comparator something like this :
class WhateverComparator {
fun getComparator() = compareBy<Whatever>({
when (it.status) {
is Whatever.Locked -> FIRST_POSITION
is Whatever.FinishToday -> SECOND_POSITION
is Whatever.DaysToExpire -> THIRD_POSITION
}
}, { it.discount })
companion object {
private const val FIRST_POSITION = 0
private const val SECOND_POSITION = 1
private const val THIRD_POSITION = 2
}
}
and it worked, but now I don't know how to estructure the object to have this distance, I thought about creating a sealed class named DistanceInfo where it can be
data class Meters(val value:Float, val symbol: String):DistanceInfo()
and also
data class KiloMeters(val value:Float, val symbol: String):DistanceInfo()
but I'm not succeeding.
The thing is that I have this symbol because it's translated by country, so it's not always "KM", that's why I need to have the symbol.
Any idea?
Edit
My class is :
data class Store(val id: String, val name: String, val distanceFormatted: String, val distanceNumber: Float, val distanceSymbol: String)
Also thinking another way to re-estructure this class to don't be like this.
DistanceFormatted ie: 34,38 Meters or 19,21 Kilometers DistanceNumber ie : 23,13 DistanceSymbol ie : Meters/Kilometers/km/m