Home > database >  How can I sort a list comparing two fields?
How can I sort a list comparing two fields?

Time:08-08

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

CodePudding user response:

Could this work for you?

class StoreComparator {

    fun getComparator() = compareBy<Store>({
        if (it.distanceFormatted.lowercase().contains("kilo")) SECOND_POSITION else FIRST_POSITION
    }, { it.distanceNumber })

    companion object {
        private const val FIRST_POSITION = 0
        private const val SECOND_POSITION = 1
    }
}

CodePudding user response:

I think it would be easier if you "standardize" the value in Meters and then convert it to Kilometers or else if needed.

data class Meters(val value:Float, val symbol: String) {
  fun toKilometers() : Kilometers = Kilometers(value = this.value / 1000f, symbol = "km")
  
  // if needed
  fun toCentimeters() : Centimeters= Centimeters(value = this.value * 100f, symbol = "cm")
}

So, when you want to sort it , you just need to sort it by the value.

  • Related