1

This is a simple situation but for some reason orderBy for me hasn't worked.

I have a very simple model class;

case class Sale(price: Int, name: String) {
  @Id
  var id: Long = 0
    @Formats.DateTime(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSZ")
    var saleDate: DateTime = new DateTime()
}

and the companion object;

object Sale {
    def find = new Finder[String, Sale](classOf[String], classOf[Sale])
}

Then I'm trying to fetch the list of all sale entries and order them using the saleDate value;

Sale.find
  .where
  ... // some conditions
  .orderBy("saleDate desc")
  .findMap

It seems pretty simple and straightforward to me, but it doesn't seem to work. Does anyone know what might the reason be?

Ashesh
  • 2,978
  • 4
  • 27
  • 47
  • Any chance you can put together a tiny project to test with? – Nate Oct 20 '14 at 18:49
  • Try to use `orderBy().desc("saleDate")` instead and enable [SQL queries logging](http://stackoverflow.com/a/11956193/1205368) so you can see what actually gets executed. – Salem Oct 20 '14 at 21:12
  • @Nate, Salem I've posted the answer. Thanks for looking into this, by the way. – Ashesh Oct 21 '14 at 07:13

1 Answers1

2

The problem is a little strange and I've found a solution. Turns out, when you do .findMap, and the keys of the resulting map are the id's of the found entries, ebean/scala decides to arrange the keys in order (in my case, ascending), completely ignoring the intended order of the found elements. So to solve this, all I needed to do was to replace .findMap with .findList.

This does bring us to an interesting possibility – arranging responses without having to do an expensive orderBy; just fetch the entries into a map with the keys being the ordering parameter. I am currently using that approach and it works perfectly. It is efficient, in fact.

Ashesh
  • 2,978
  • 4
  • 27
  • 47