0

I am trying to create a messaging app in Android and I need to sort the messages according to their timestamps, which I created from a ZonedDateTime object. I was going to parse the timestamp strings with the formatter I used to re-create a ZonedDateTime object, but I need to be able to sort the arraylist of ZonedDateTime objects in chronological order. How can I do this?

biotecher
  • 159
  • 4
  • 14
  • Possibly related: [Java - Result when compare two ZonedDateTime is not as expected](https://stackoverflow.com/questions/49686272/java-result-when-compare-two-zoneddatetime-is-not-as-expected) – Ole V.V. Aug 15 '20 at 11:05

1 Answers1

0

sorted() is the way to sort items in a list.

sort() will sort a mutable list or array.

In the Kotlin documentation, there are allusions to sorting by a natural order which only means that the class needs to implement the Comparable interface. ZonedDateTime implements ChronoZonedDateTime which implements Comparable so these methods should work.

Steph
  • 831
  • 6
  • 19