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...
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.
If you have a list of String
s 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With