Home > Enterprise >  how to use sortBy in kotlin
how to use sortBy in kotlin

Time:02-17

Hey I want to sort the data class in kotlin. I tried to sort by ascending order, but it not sorting correctly.

ProductVariant.kt

data class ProductVariant(
    val strength: String? = null,
    val quantity: String? = null,
    val subscription: String? = null
)

First I need to sort by strength value then after sorting, I want to sort by quantity value in ascending order. I tried some code to sort first by strength it's not working.

Options.kt

fun main() {
    val list = getUnSortedDataList()

    val sortedList = list.sortedBy {
        it.subscription
    }

    sortedList.forEach {
        println(it)
    }
}

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")
    )
}

Getting output through above code is

ProductVariant(strength=75, quantity=14, subscription=1)
ProductVariant(strength=25, quantity=8, subscription=1)
ProductVariant(strength=50, quantity=14, subscription=1)
ProductVariant(strength=75, quantity=10, subscription=1)
ProductVariant(strength=25, quantity=2, subscription=1)
ProductVariant(strength=25, quantity=4, subscription=1)
ProductVariant(strength=25, quantity=6, subscription=1)
ProductVariant(strength=25, quantity=10, subscription=1)
ProductVariant(strength=25, quantity=12, subscription=1)
ProductVariant(strength=25, quantity=14, subscription=1)
ProductVariant(strength=50, quantity=2, subscription=1)
ProductVariant(strength=50, quantity=4, subscription=1)
ProductVariant(strength=50, quantity=6, subscription=1)
ProductVariant(strength=50, quantity=10, subscription=1)
ProductVariant(strength=50, quantity=12, subscription=1)
ProductVariant(strength=75, quantity=2, subscription=1)
ProductVariant(strength=75, quantity=6, subscription=1)
ProductVariant(strength=75, quantity=8, subscription=1)
ProductVariant(strength=75, quantity=12, subscription=1)
ProductVariant(strength=50, quantity=8, subscription=1)
ProductVariant(strength=75, quantity=4, subscription=1)
ProductVariant(strength=75, quantity=14, subscription=12)
ProductVariant(strength=25, quantity=8, subscription=12)
ProductVariant(strength=50, quantity=14, subscription=12)
ProductVariant(strength=75, quantity=10, subscription=12)
ProductVariant(strength=25, quantity=2, subscription=12)
ProductVariant(strength=25, quantity=4, subscription=12)
ProductVariant(strength=25, quantity=6, subscription=12)
ProductVariant(strength=25, quantity=10, subscription=12)
ProductVariant(strength=25, quantity=12, subscription=12)
ProductVariant(strength=25, quantity=14, subscription=12)
ProductVariant(strength=50, quantity=2, subscription=12)
ProductVariant(strength=50, quantity=4, subscription=12)
ProductVariant(strength=50, quantity=6, subscription=12)
ProductVariant(strength=50, quantity=10, subscription=12)
ProductVariant(strength=50, quantity=12, subscription=12)
ProductVariant(strength=75, quantity=2, subscription=12)
ProductVariant(strength=75, quantity=6, subscription=12)
ProductVariant(strength=75, quantity=8, subscription=12)
ProductVariant(strength=75, quantity=12, subscription=12)
ProductVariant(strength=50, quantity=8, subscription=12)
ProductVariant(strength=75, quantity=4, subscription=12)
ProductVariant(strength=75, quantity=14, subscription=3)
ProductVariant(strength=25, quantity=8, subscription=3)
ProductVariant(strength=50, quantity=14, subscription=3)
ProductVariant(strength=75, quantity=10, subscription=3)
ProductVariant(strength=25, quantity=2, subscription=3)
ProductVariant(strength=25, quantity=4, subscription=3)
ProductVariant(strength=25, quantity=6, subscription=3)
ProductVariant(strength=25, quantity=10, subscription=3)
ProductVariant(strength=25, quantity=12, subscription=3)
ProductVariant(strength=25, quantity=14, subscription=3)
ProductVariant(strength=50, quantity=2, subscription=3)
ProductVariant(strength=50, quantity=4, subscription=3)
ProductVariant(strength=50, quantity=6, subscription=3)
ProductVariant(strength=50, quantity=10, subscription=3)
ProductVariant(strength=50, quantity=12, subscription=3)
ProductVariant(strength=75, quantity=2, subscription=3)
ProductVariant(strength=75, quantity=6, subscription=3)
ProductVariant(strength=75, quantity=8, subscription=3)
ProductVariant(strength=75, quantity=12, subscription=3)
ProductVariant(strength=50, quantity=8, subscription=3)
ProductVariant(strength=75, quantity=4, subscription=3)
ProductVariant(strength=75, quantity=14, subscription=6)
ProductVariant(strength=25, quantity=8, subscription=6)
ProductVariant(strength=50, quantity=14, subscription=6)
ProductVariant(strength=75, quantity=10, subscription=6)
ProductVariant(strength=25, quantity=2, subscription=6)
ProductVariant(strength=25, quantity=4, subscription=6)
ProductVariant(strength=25, quantity=6, subscription=6)
ProductVariant(strength=25, quantity=10, subscription=6)
ProductVariant(strength=25, quantity=12, subscription=6)
ProductVariant(strength=25, quantity=14, subscription=6)
ProductVariant(strength=50, quantity=2, subscription=6)
ProductVariant(strength=50, quantity=4, subscription=6)
ProductVariant(strength=50, quantity=6, subscription=6)
ProductVariant(strength=50, quantity=10, subscription=6)
ProductVariant(strength=50, quantity=12, subscription=6)
ProductVariant(strength=75, quantity=2, subscription=6)
ProductVariant(strength=75, quantity=6, subscription=6)
ProductVariant(strength=75, quantity=8, subscription=6)
ProductVariant(strength=75, quantity=12, subscription=6)
ProductVariant(strength=50, quantity=8, subscription=6)
ProductVariant(strength=75, quantity=4, subscription=6)
ProductVariant(strength=75, quantity=14, subscription=9)
ProductVariant(strength=25, quantity=8, subscription=9)
ProductVariant(strength=50, quantity=14, subscription=9)
ProductVariant(strength=75, quantity=10, subscription=9)
ProductVariant(strength=25, quantity=2, subscription=9)
ProductVariant(strength=25, quantity=4, subscription=9)
ProductVariant(strength=25, quantity=6, subscription=9)
ProductVariant(strength=25, quantity=10, subscription=9)
ProductVariant(strength=25, quantity=12, subscription=9)
ProductVariant(strength=25, quantity=14, subscription=9)
ProductVariant(strength=50, quantity=2, subscription=9)
ProductVariant(strength=50, quantity=4, subscription=9)
ProductVariant(strength=50, quantity=6, subscription=9)
ProductVariant(strength=50, quantity=10, subscription=9)
ProductVariant(strength=50, quantity=12, subscription=9)
ProductVariant(strength=75, quantity=2, subscription=9)
ProductVariant(strength=75, quantity=6, subscription=9)
ProductVariant(strength=75, quantity=8, subscription=9)
ProductVariant(strength=75, quantity=12, subscription=9)
ProductVariant(strength=50, quantity=8, subscription=9)
ProductVariant(strength=75, quantity=4, subscription=9)

I want like this

Expected output

        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 = "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 = "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 = "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 = "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 = "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 = "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 = "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"),
        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 = "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 = "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 = "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")

CodePudding user response:

val sortedList = list.sortedWith(
  compareBy<ProductVariant> {   // or compareByDescending
    it.strength?.toInt() ?: 0   // or java.lang.Integer.MAX_VALUE
  }.thenBy {                    // or thenByDescending
    it.quantity?.toInt() ?: 0   // or java.lang.Integer.MAX_VALUE
  }
)

CodePudding user response:

You sort strings, not numbers and by default strings are sorted in lexicographic order. You just need to convert these values to integers:

list.sortedBy { it.strength?.toInt() } 

CodePudding user response:

Use sortedWith compareBy:

val sortedList = list.sortedWith(compareBy({ it.strength?.toInt() ?: 0 }, { it.quantity?.toInt() ?: 0 }, { it.subscription?.toInt() ?: 0 }))
  • Related