Q:

Casting string to date for sorting

I have a sort on an ArrayList;
    matchesList.sort(Comparator.comparing(matches::getDate).thenComparing(matches::getTime));
The value comes from a Firebase database so the value is stored as a String instead of Date (I don't want to have to store the date value as long format) in the following format; dd/MM/yyyy
The sort above does not work as expected, I'm guessing after reading through other posts that this is due to the comparator not knowing what the String date value actually means so cannot sort it as expected.
After some reading and playing around with, ideally I'd like to convert the class that defines my ArrayList structure to support the Date type but when trying this I get an error about cannot convert a string value to date when looping the dataset and trying to populate the ArrayList;
matches match = matchSnapshot.getValue(matches.class);

Again another post talks about this being due to Firebase not supporting Date types, but I've also tried using *.toString on Date type variables and still it doesn't like this.
My question is; is there a way to store the date format dd/MM/yyyy as String and then to cast that to date that can be added to an ArrayList?

A:

I would suggest you to cast your dd/MM/yyyy format String to timestamp (long) and then sorting timestamps (you can use Collections.sort() for that). More on timestamps here: Convert string Date into timestamp in Android?
Then, when you need to use those sorted values again, just cast them back to date. More about this here: Convert timestamp into string Date in Android?

Hope it helps.

