18

I have a list of objects like this:

 [
    {
      "Price": 2100000,
      "Id": "5f53787e871ebc4bda455927"
    },
    {
      "Price": 2089000,
      "Id": "5f7da4ef7a0ad2ed730416f8"
    },
    {
   
      "Price": 0,
      "Id": "5f82b1189c333dab0b1ce3c5"
    }
 ]

How can I sort this list by price value of objects and then pass it to my adapter?

3 Answers 3

31

If you have an object like this:

class YourClass(
  val price: Int,
  val id: String
)

You can sort by price in two ways:

Mutable

val yourMutableList: MutableList<YourClass> = mutableListOf()
yourMutableList.sortBy { it.price }
// now yourMutableList is sorted itself

Not mutable

val yourList: List<YourClass> = listOf()
val yourSortedList: List<YourClass> = yourList.sortedBy { it.price }

As you can see, in the second sample, you have to save the result in a new list. Because List is immutable, therefore it cannot be altered and it is necessary to create a new list.

Happy coding! :)

Sign up to request clarification or add additional context in comments.

5 Comments

thanks for your answer. Im new to kotlin so I have to ask for a little more clarification. I am getting the list like this: ` productDetailViewModel.productLiveData.observe(viewLifecycleOwner) { item -> productStoresAdapter.stores = item.sameProducts as ArrayList<SameProduct>` Here I need to sort 'item.sameProducts' with the values I wrote on the question. Would you please help me more on this?
You can save first your List and then sort it. For example: List<SameProduct> sameProducts = item.sameProducts as ArrayList<SameProduct>, and then put the list sorted in your adapter, productStoresAdapter.stores = sameProducts.sortedBy { it.Price }
Dear Jose I did what you said but I got this error: Caused by: java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList
In order to help you I would need to know the type of item.sameProducts.
Please, complete your main question above with more detail about SameProduct class and the call to your ViewModel. :)
6

Here's a very simple example to sort by your choice of order:

class Pizza(
  val price: Double
)

val regularSlice = Pizza(price = 2.75)
val extraCheeseSlice = Pizza(price = 3.75)
val pepperoniSlice = Pizza(price = 4.75)

val list = ArrayList<Pizza>()
list.add(extraCheeseSlice)
list.add(pepperoniSlice)
list.add(regularSlice)

This is the same exact list sorted in place:

list.sortBy { it.price } // lowest to highest - result is: regularSlice, extraCheeseSlice, pepperoniSlice

list.sortByDescending { it.price } // highest to lowest - result is: pepperoniSlice, extraCheeseSlice, regularSlice

If you want a new list just use sortedBy:

val newListFromLowestToHighest = list.sortedBy { it.price } // result is: regularSlice, extraCheeseSlice, pepperoniSlice
    
val newListFromHighestToLowest = list.sortByDescending { it.price } // result is: pepperoniSlice, extraCheeseSlice, regularSlice

Comments

3

Assuming you have a class that represents your elements like this:

data class Element (
    val price: Int, 
    val id: String
)

And given a list of those from your deserializer:

val listOfElements: List<Element> = ...

You can obtain a new list sorted by price as follows:

val sortedByPrice = listOfElements.sortedBy { it.price }

Try it in the Kotlin Playground: https://pl.kotl.in/TBIdxsaoi

1 Comment

thanks for your answer. Im new to kotlin so I have to ask for a little more clarification. I am getting the list like this: ` productDetailViewModel.productLiveData.observe(viewLifecycleOwner) { item -> productStoresAdapter.stores = item.sameProducts as ArrayList<SameProduct>` Here I need to sort 'item.sameProducts' with the values I wrote on the question. Would you please help me more on this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.