Home > Enterprise >  Sort a list of Class by a specific attribute [duplicate]
Sort a list of Class by a specific attribute [duplicate]

Time:09-22

Hi I'm new in android development (kotlin), here is my problem: I have an ArrayList class (ProductStore), in this class there is another class (StorageType) in which there is a string (libellus) with which I want to sort my ArrayList (or rather constitute groups where the string (libellus) is identical. How can I do it?

data class Stock(
    val product: ArrayList<ProductStore>
)

class ProductStore {
    var quantity: Double? = null
    val expiration_date: String? = null
    val product: Product? = null
    val storage_type: StorageType? = null
    val quantity_type: QuantityType? = null
}

class Product{
    val id: Double? =  null
    val name: String? = null
    val image: String? =null
}

class StorageType{
    val id: Double? = null
    val libellus: String? = null
}

class QuantityType{
    val libellus: String? = null
}

CodePudding user response:

The question title is a good hint on what you need to do ;-) Just use sortedBy() function:

val list: List<ProductStore> = ...

val sorted = list.sortedBy { it.storage_type?.libellus }

You said you use ArrayList, so if you prefer to sort in-place then you can use sortBy() instead.

Depending on where do you need to place null items (on top or on the bottom), you may need to modify this code.

or rather constitute groups where the string (libellus) is identical

If this is the case then you may not need to do sorting at all. You can group items like this:

val grouped = list.groupBy { it.storage_type?.libellus }

It gives you a map where keys are libellus strings and values are lists of their associated ProductStore objects.

CodePudding user response:

I tried to recreate your code inside main method. But first I had to convert ProductStore(), Product(), StorageType() and QuantityType() normal Kotlin Classes to Data Classes in order to easily print them as a String using the implicit toString().

I then made an example of the Stock Data Class as follows:

fun main() {
    
    //instance of the Stock Class
    val stock = Stock(
        product = arrayListOf(
            ProductStore(
                storage_type = StorageType(libellus = "frozen"),
                quantity = 13.0,
                expiration_date = "",
                product = Product(),
                quantity_type = QuantityType()
            ), ProductStore(
                storage_type = StorageType(libellus = "fresh"),
                quantity = 18.0,
                expiration_date = "",
                product = Product(),
                quantity_type = QuantityType()
            ), ......}}

From here I just used @broot approach to group the ArrayList of ProductStore elements into a Map Object:

//grouping  the stock
    val productsGroups = stock.product.groupBy { productStore ->
        when (productStore.storage_type?.libellus) {

            "frozen" -> {
                "frozen"
            }
            "fresh" -> {
                "fresh"
            }
            else -> {
                "ambient"
            }

        }
    }
    println(productsGroups)
}

The output of println(productsGroups) was a printed Map Object whereby the Stock's StorageType (frozen, ambient, fresh) was keys and corresponding lists as the values.

{frozen=[ProductStore(quantity=13.0, expiration_date=, product=Product(id=null, name=null, image=null), storage_type=StorageType(id=null, libellus=frozen), quantity_type=QuantityType(libellus=null))], 

fresh=[ProductStore(quantity=18.0, expiration_date=, product=Product(id=null, name=null, image=null), storage_type=StorageType(id=null, libellus=fresh), quantity_type=QuantityType(libellus=null))], 

ambient=[ProductStore(quantity=2021.0, expiration_date=, product=Product(id=null, name=null, image=null), storage_type=StorageType(id=null, libellus=ambient), quantity_type=QuantityType(libellus=null))]}
  • Related