0

I have data from json file which I display in recyclerview in my app. I'm trying to sort this data by year. That's how my code looks:

In MainActivity.kt everythings happend in fetchJson() function

private fun fetchJson(jsonUrl: String) {
    Log.d(TAG, "Attempting to fetch json")

    val request = okhttp3.Request.Builder().url(jsonUrl).build()

    val client = OkHttpClient()
    client.newCall(request).enqueue(object: Callback {
        override fun onFailure(call: Call, e: IOException) {
            Log.d(TAG, "Failed to execute request")
        }

        override fun onResponse(call: Call, response: Response) {
            val body = response.body()?.string()
            Log.d(TAG, "$body")

            val gson = GsonBuilder().create()

            val homeFeed = gson.fromJson(body, HomeFeed::class.java)
            homeFeed.standups.sortedWith(compareBy({it.year}))

            runOnUiThread {
                rv.adapter = Adapter(homeFeed)
            }
        }
    })
}

    fun <T> compareBy(vararg selectors: (T) -> Comparable<*>?): Comparator<T> {
        return Comparator<T> { a, b -> compareValuesBy(a, b, *selectors) }
    }

class HomeFeed is here:

class HomeFeed(val standups: List<StandUps>)

and data class StandUps:

data class StandUps(
        val artist: String,
        val title: String,
        val year: String,
        val poster: String,
        val description: String,
        val netflix_link: String,
        val imdb_rate: String,
        val imdb_link: String,
        val duration_min: String
)

It doesn't shows any errors or warnings, it just doesn't do anything. How could I achieve this?

3 Answers 3

2

You have to first store the sorted list in another variable and then use that variable to pass it to your adapter

val homeFeed = gson.fromJson(body, HomeFeed::class.java)
            val sortedHomeFeed = homeFeed.standups.sortedWith(compareBy({it.year}))

            runOnUiThread {
                rv.adapter = Adapter(sortedHomeFeed)
            }

The reason for this is, changes are not made to the original list following the concepts of immutability.

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

2 Comments

Thanks! Works perfect :)
Great! Cheers :D
1

If you want to sort your list ascending by a year you can do this:

val sortedStandUps = homeFeed.standups.sortedBy { it.year }

If you want to sort list descending do this:

val sortedStandUps = homeFeed.standups.sortedByDescending { it.year }

Comments

0

Kotlin gives you easy sorting. Jus like below

make a temp object (i.e)., tempFilterData here

val standUps = tempFilterData?.sortedWith(compareBy({ it.Year }))

Now you can get the sorted data based on YEAR

Comments

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.