18

I have arraylist typeBeanArrayList where element is some like a date: for example:

[30-03-2012, 28-03-2013, 31-03-2012, 2-04-2012, ...]

How can I sort in descending order.

Code:

typeBeanArrayList = database.getSingleCustomerDetail(c_id!!) //get data from SQlite database

creditListAdapter = CreditListAdapter(typeBeanArrayList)
rv_credit_list!!.adapter = creditListAdapter //Bind data in adapter

Thanks in advance...

1
  • I don't know much about kotlin but I'm pretty sure you could write your own comparator kotlination.com/kotlin/… Commented Feb 28, 2019 at 12:43

5 Answers 5

36

Thank you @svkaka for information.

I just notice one line from @svkaka answer : .sortByDescending { it.length }.

and i changes in my code like :

typeBeanArrayList.sortByDescending{it.date}

Sorting is perfectly work.

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

4 Comments

How does it sort by date ? This answer is wrong it simply takes String length and sorts it in descending order
Really it doesn't work for date format, this solution checks for string as date it it.date is not a Date object
i dont know how but this works. I wonder if there are any failing edge cases.
i believe you need to convert to a date object; otherwise you are comparing strings; might be wrong though
12

If you have a list of Strings representing dates, one way to sort them in descending order is:

val dates = listOf("30-03-2012", "28-03-2013", "31-03-2012", "2-04-2012")

val dateTimeFormatter: DateTimeFormatter = DateTimeFormatter.ofPattern("dd-MM-yyyy")

val result = dates.sortedByDescending {
    LocalDate.parse(it, dateTimeFormatter)
}

println(result)

This will print:

[28-03-2013, 2-04-2012, 31-03-2012, 30-03-2012]

Note that sortedByXXX methods return a new List, i.e., they don't sort in place

7 Comments

yes, the first item belongs to year 2013, all the others belong to 2012
this is long way i just use one line : typeBeanArrayList.sortByDescending{it.date} and this is work perfectly. thanks for your response.
i just little bit confuse i just watch dates but there is also consider month for descending.
Yep, your code works and is shorter, and that's because your list doesn't contain Strings but instances of some kind of type that has a date property (returning a Date or LocalDate object, I guess)
@MR.K I'm sorry but I'm not sure I fully understood: is date of type String? If that's the case, then you're sorting alphabetically instead of chronologically (which may be what you want, though)
|
3

Use

[30-03-2012, 28-03-2013, 31-03-2012, 2-04-2012, ...].sortDescending()

if you want to sort by param use in this case length

[30-03-2012, 28-03-2013, 31-03-2012, 2-04-2012, ...].sortByDescending { it.length }

2 Comments

I think the OP wants to sort by chronological descending order, which means they can't use .sortDescending() as it will sort by alphabetical order. They need to use sortByDescending, transforming the String to a Date object, which can then be sorted as desired
i just use typeBeanArrayList.sortByDescending{it.date} and data will show in Descending order. Thank you @svkaka
3

You can use something like this if you are using a custom model

typeBeanArrayList.sortedWith(compareByDescending<YourModel> { it.date() })

Comments

0
data class YouModel(val startDate: String? = null) {

  val BY_DATE_ORDER =
            compareByDescending<YourModel> {
                val format = SimpleDateFormat("yyyy-MM-dd")
                var convertedDate = Date()
                try {
                    convertedDate = it.startDate?.let { it1 -> format.parse(it1) } as Date
                } catch (e: ParseException) {
                    e.printStackTrace()
                }

                convertedDate
            }
    }



yourList<YourModel>?.sortedWith(YourModel.BY_DATE_ORDER)

This works for me. I created a descending comparator using the function to transform value to a Comparable instance for comparison. I converted the string date to date before applying the sorting function.

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.