Reputation: 43
I'm looking for a functional way to sort Dates in Scala. The dates come in as a list of strings and need to return as a sorted list of strings. The list of strings are of these formats:
"dd MMM yyyy"
"MMM yyyy"
"yyyy"
""
I have tried setting up a regex to parse for the day, month, and year. I would then put them into year month day and use the .sorted . This would however place 1 before 10 and couldn't handle the months. Jan, Feb, ...
I then tried converting the strings into date classes but I couldn't find any way to sort them.
I have a considerable amount of data to sort. That would be the only consideration.
Thank you, Erick
Upvotes: 3
Views: 7395
Reputation: 35463
If you can correctly convert the list of String
to a List[Date]
, then you are really close. From there, assuming the list is called list
, you can sort by the epoch time of the dates:
list.sortBy(_.getTime)
Upvotes: 4