Hey I am sorting my reponse throught `sortedWith` ...
# android
v
Hey I am sorting my reponse throught
sortedWith
+
compareBy
is any other way in kotlin to do that which better in performance, speed, memory management etc.
Copy code
val list = getUnSortedDataList()

val sortedList = list.sortedWith(
    compareBy<ProductVariant> {
        it.strength?.toInt()
    }.thenBy {
        it.quantity?.toInt()
    }
)
data class ProductVariant(
    val strength: String? = null,
    val quantity: String? = null,
    val subscription: String? = null
)
Copy code
fun getUnSortedDataList(): List<ProductVariant> {
    return listOf(
        ProductVariant(strength = "75", quantity = "14", subscription = "1"),
        ProductVariant(strength = "75", quantity = "14", subscription = "3"),
        ProductVariant(strength = "75", quantity = "14", subscription = "6"),
        ProductVariant(strength = "75", quantity = "14", subscription = "9"),
        ProductVariant(strength = "75", quantity = "14", subscription = "12"),
        ProductVariant(strength = "25", quantity = "8", subscription = "1"),
        ProductVariant(strength = "25", quantity = "8", subscription = "3"),
        ProductVariant(strength = "25", quantity = "8", subscription = "6"),
        ProductVariant(strength = "25", quantity = "8", subscription = "9"),
        ProductVariant(strength = "25", quantity = "8", subscription = "12"),
        ProductVariant(strength = "50", quantity = "14", subscription = "1"),
        ProductVariant(strength = "50", quantity = "14", subscription = "3"),
        ProductVariant(strength = "50", quantity = "14", subscription = "6"),
        ProductVariant(strength = "50", quantity = "14", subscription = "9"),
        ProductVariant(strength = "50", quantity = "14", subscription = "12"),
        ProductVariant(strength = "75", quantity = "10", subscription = "1"),
        ProductVariant(strength = "75", quantity = "10", subscription = "3"),
        ProductVariant(strength = "75", quantity = "10", subscription = "6"),
        ProductVariant(strength = "75", quantity = "10", subscription = "9"),
        ProductVariant(strength = "75", quantity = "10", subscription = "12"),
        ProductVariant(strength = "25", quantity = "2", subscription = "1"),
        ProductVariant(strength = "25", quantity = "2", subscription = "3"),
        ProductVariant(strength = "25", quantity = "2", subscription = "6"),
        ProductVariant(strength = "25", quantity = "2", subscription = "9"),
        ProductVariant(strength = "25", quantity = "2", subscription = "12"),
        ProductVariant(strength = "25", quantity = "4", subscription = "1"),
        ProductVariant(strength = "25", quantity = "4", subscription = "3"),
        ProductVariant(strength = "25", quantity = "4", subscription = "6"),
        ProductVariant(strength = "25", quantity = "4", subscription = "9"),
        ProductVariant(strength = "25", quantity = "4", subscription = "12"),
        ProductVariant(strength = "25", quantity = "6", subscription = "1"),
        ProductVariant(strength = "25", quantity = "6", subscription = "3"),
        ProductVariant(strength = "25", quantity = "6", subscription = "6"),
        ProductVariant(strength = "25", quantity = "6", subscription = "9"),
        ProductVariant(strength = "25", quantity = "6", subscription = "12"),
        ProductVariant(strength = "25", quantity = "10", subscription = "1"),
        ProductVariant(strength = "25", quantity = "10", subscription = "3"),
        ProductVariant(strength = "25", quantity = "10", subscription = "6"),
        ProductVariant(strength = "25", quantity = "10", subscription = "9"),
        ProductVariant(strength = "25", quantity = "10", subscription = "12"),
        ProductVariant(strength = "25", quantity = "12", subscription = "1"),
        ProductVariant(strength = "25", quantity = "12", subscription = "3"),
        ProductVariant(strength = "25", quantity = "12", subscription = "6"),
        ProductVariant(strength = "25", quantity = "12", subscription = "9"),
        ProductVariant(strength = "25", quantity = "12", subscription = "12"),
        ProductVariant(strength = "25", quantity = "14", subscription = "1"),
        ProductVariant(strength = "25", quantity = "14", subscription = "3"),
        ProductVariant(strength = "25", quantity = "14", subscription = "6"),
        ProductVariant(strength = "25", quantity = "14", subscription = "9"),
        ProductVariant(strength = "25", quantity = "14", subscription = "12"),
        ProductVariant(strength = "50", quantity = "2", subscription = "1"),
        ProductVariant(strength = "50", quantity = "2", subscription = "3"),
        ProductVariant(strength = "50", quantity = "2", subscription = "6"),
        ProductVariant(strength = "50", quantity = "2", subscription = "9"),
        ProductVariant(strength = "50", quantity = "2", subscription = "12"),
        ProductVariant(strength = "50", quantity = "4", subscription = "1"),
        ProductVariant(strength = "50", quantity = "4", subscription = "3"),
        ProductVariant(strength = "50", quantity = "4", subscription = "6"),
        ProductVariant(strength = "50", quantity = "4", subscription = "9"),
        ProductVariant(strength = "50", quantity = "4", subscription = "12"),
        ProductVariant(strength = "50", quantity = "6", subscription = "1"),
        ProductVariant(strength = "50", quantity = "6", subscription = "3"),
        ProductVariant(strength = "50", quantity = "6", subscription = "6"),
        ProductVariant(strength = "50", quantity = "6", subscription = "9"),
        ProductVariant(strength = "50", quantity = "6", subscription = "12"),
        ProductVariant(strength = "50", quantity = "10", subscription = "1"),
        ProductVariant(strength = "50", quantity = "10", subscription = "3"),
        ProductVariant(strength = "50", quantity = "10", subscription = "6"),
        ProductVariant(strength = "50", quantity = "10", subscription = "9"),
        ProductVariant(strength = "50", quantity = "10", subscription = "12"),
        ProductVariant(strength = "50", quantity = "12", subscription = "1"),
        ProductVariant(strength = "50", quantity = "12", subscription = "3"),
        ProductVariant(strength = "50", quantity = "12", subscription = "6"),
        ProductVariant(strength = "50", quantity = "12", subscription = "9"),
        ProductVariant(strength = "50", quantity = "12", subscription = "12"),
        ProductVariant(strength = "75", quantity = "2", subscription = "1"),
        ProductVariant(strength = "75", quantity = "2", subscription = "3"),
        ProductVariant(strength = "75", quantity = "2", subscription = "6"),
        ProductVariant(strength = "75", quantity = "2", subscription = "9"),
        ProductVariant(strength = "75", quantity = "2", subscription = "12"),
        ProductVariant(strength = "75", quantity = "6", subscription = "1"),
        ProductVariant(strength = "75", quantity = "6", subscription = "3"),
        ProductVariant(strength = "75", quantity = "6", subscription = "6"),
        ProductVariant(strength = "75", quantity = "6", subscription = "9"),
        ProductVariant(strength = "75", quantity = "6", subscription = "12"),
        ProductVariant(strength = "75", quantity = "8", subscription = "1"),
        ProductVariant(strength = "75", quantity = "8", subscription = "3"),
        ProductVariant(strength = "75", quantity = "8", subscription = "6"),
        ProductVariant(strength = "75", quantity = "8", subscription = "9"),
        ProductVariant(strength = "75", quantity = "8", subscription = "12"),
        ProductVariant(strength = "75", quantity = "12", subscription = "1"),
        ProductVariant(strength = "75", quantity = "12", subscription = "3"),
        ProductVariant(strength = "75", quantity = "12", subscription = "6"),
        ProductVariant(strength = "75", quantity = "12", subscription = "9"),
        ProductVariant(strength = "75", quantity = "12", subscription = "12"),
        ProductVariant(strength = "50", quantity = "8", subscription = "1"),
        ProductVariant(strength = "50", quantity = "8", subscription = "3"),
        ProductVariant(strength = "50", quantity = "8", subscription = "6"),
        ProductVariant(strength = "50", quantity = "8", subscription = "9"),
        ProductVariant(strength = "50", quantity = "8", subscription = "12"),
        ProductVariant(strength = "75", quantity = "4", subscription = "1"),
        ProductVariant(strength = "75", quantity = "4", subscription = "3"),
        ProductVariant(strength = "75", quantity = "4", subscription = "6"),
        ProductVariant(strength = "75", quantity = "4", subscription = "9"),
        ProductVariant(strength = "75", quantity = "4", subscription = "12")
    )
}
m
the toInt on strings every call might be whats the most expensive here, so If anything I would add a body to your dataclass with already translated int variants of your fields (although all your fields seem to be both an Int and Non-Nullable to begin with, so maybe change data types first?)
v
this reponse is coming from server and value is coming in string form
z
You could always map it to your own local model before sorting them, something like
Copy code
list.map { BetterProductVariant(it.strength?.toInt(), ...) }
   .sortedWith(...)
b
if you need a most performant way, using pure comparator / overriding compareTo is a way to go. compareBy uses generics, so it will always box your int to non-primitive Int. Additionally, compareBy will invoke these lamdas on each comparison, so as was advised above, parse int before sorting. But overall, if your list is less than 100 elements (as in your example) and you call it rare, I don't think you will see noticeable perf improvements
v
thanks everyone
@zsmb what is BetterProductVariant is this new class?
@zsmb I am trying some code but it giving me error
Copy code
getUnSortedDataList.map {
    ProductVariant(it.strength.value.toInt() , it.quantity.value?.toInt())
}
z
Yes, that was just my proposed example name for a new class that would contain the same properties but with different types,
Int?
in this case
v
ok sure thanks
got it