Search code examples
scalaslick

Sorting by DateTime in Slick


I'm currently going through a rough point in Slick. I'm trying to sort the query of a table with a timestamp:

TableName.filter(tableAttribute === 1).sortBy(_.tableTimestamp)

The timestamp is of type joda.DateTime within slick. When I try to sort, I'm getting the following error:

No implicit view available from dao.Tables.profile.api.Rep[org.joda.time.DateTime] => slick.lifted.Ordered.

I'm assuming that this isn't built into Slick. Is there a quick and clean way to add an implicit view and solve this?

Thanks!


Solution

  • You might be looking for an implicit conversion using Ordering.fromLessThan like below:

    import org.joda.time.DateTime
    
    implicit def datetimeOrdering: Ordering[DateTime] = Ordering.fromLessThan(_ isBefore _)
    

    In case you want to reverse the ordering, simply replace isBefore with isAfter.