3

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

user2506494
  • 43
  • 1
  • 4

1 Answers1

4

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)
cmbaxter
  • 35,283
  • 4
  • 86
  • 95
  • And this answer: http://stackoverflow.com/a/16883858/2483228 might be useful in obtaining the list... – Mark Lister Jul 09 '13 at 21:37
  • Thank you ^_^ I spent a considerable amount of time looking around and testing different things. The method would work if I converted everything to YYYYMMDD with the Month being changed to 01,02, etc... – user2506494 Jul 10 '13 at 00:27