Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 261 Vote(s) - 3.56 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sort list with string date [Kotlin]

#1
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...
Reply

#2
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 }
Reply

#3
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.

Reply

#4
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*
Reply

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

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

#6

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.
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through