-1

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
}

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
dja
  • 53
  • 6
  • I think this could help you https://stackoverflow.com/a/65169017/13373575 – MadMax Sep 17 '21 at 07:36
  • 1
    Does this answer your question? [How to sort an array of object in kotlin with custom order?](https://stackoverflow.com/questions/59402219/how-to-sort-an-array-of-object-in-kotlin-with-custom-order) – gioravered Sep 17 '21 at 12:25

2 Answers2

1

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.

broot
  • 21,588
  • 3
  • 30
  • 35
  • Thank you for this answer it helps me a lot however as I know the value "libelus" is going to be (fresh, ambient or frozen) I would like to create groups from these values – dja Sep 17 '21 at 08:20
  • I'm not sure if I understand you correctly. It sounds like `groupBy()` should do exactly what you need - it will give you: `fresh`, `ambient`, etc. groups. – broot Sep 17 '21 at 08:23
0

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))]}
Dharman
  • 30,962
  • 25
  • 85
  • 135
Tonnie
  • 4,865
  • 3
  • 34
  • 50