5

The following function works fine, but I would like it to sort the results first by parent_id, and then by order.

def getTree = for {
  (a, c) <- Activities leftJoin Clients on (_.id === _.id_a)
} yield (a.id, a.label, a.parent_id, a.order, c.id.?, a=c.name)

How do I do that using Slick?

Jack
  • 16,506
  • 19
  • 100
  • 167

2 Answers2

10

Like with ordinary collection ?

getTree.sortBy(r => r._3 ~ r._4)
idonnie
  • 1,703
  • 12
  • 11
  • 1
    How intuitive the tuple syntax is...not. Too bad old orderBy(nameThatHasMeaning, otherMeaningfulName.desc) has been deprecated in favor of sortBy(x=> x._3 ~ x._4) syntax. Slick author had a reason for doing so, but end users suffer boilerplate and pointless counting out which tuple number as a result. – virtualeyes Jan 17 '13 at 00:02
  • You could `yield` some case class instance, and `getTree.sortBy(r => r.parent_id ~ r.order)`, but actual declaration of case class is a boilerplate. Maybe named `Tuple`-s, with constructor like `('name1 -> value1, 'name2 -> value2)` and accessor like `apply(name: Symbol)` will be useful in core Scala? For example: `yield (a.id, a.label, 'parent_id -> a.parent_id, 'order -> a.order, c.id.?, a=c.name)`, then `getTree.sortBy(r => r('parent_id) ~ r('order))` – idonnie Jan 17 '13 at 00:24
  • Is this answer still valid for Slick 2.1? IDEA is not finding the ~ operator. Or am I missing some magic import statement? – Andrew Swan Sep 09 '15 at 01:53
2

With Slick 2.1, I found this to work:

myQuery.sortBy(r => (r._3, r._4))

(verified by calling selectStatement on my query)

Andrew Swan
  • 13,427
  • 22
  • 69
  • 98