Vivek Modi
02/16/2022, 3:59 PMsortedWith
+ compareBy
is any other way in kotlin to do that which better in performance, speed, memory management etc.
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
)
Vivek Modi
02/16/2022, 3:59 PMfun 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")
)
}
Michael de Kaste
02/16/2022, 4:20 PMVivek Modi
02/16/2022, 4:27 PMzsmb
02/16/2022, 4:56 PMlist.map { BetterProductVariant(it.strength?.toInt(), ...) }
.sortedWith(...)
bezrukov
02/16/2022, 5:12 PMVivek Modi
02/17/2022, 4:53 PMVivek Modi
02/17/2022, 4:55 PMVivek Modi
02/17/2022, 4:57 PMgetUnSortedDataList.map {
ProductVariant(it.strength.value.toInt() , it.quantity.value?.toInt())
}
zsmb
02/17/2022, 4:58 PMInt?
in this caseVivek Modi
02/17/2022, 4:59 PMVivek Modi
02/17/2022, 4:59 PM