[Slick1.0-RC1] Multiple field sorting

717 views
Skip to first unread message

Pradeep Kumar Mishra

unread,
Feb 6, 2013, 5:31:25 PM2/6/13
to scala...@googlegroups.com
I am trying to implement sorting over two fields lets say price, rating in order of asc, desc respectively. I see the results are not appropriate and after I print the statement I see the order is reversed i.e. I am geting statement ending with order by x23.x30 desc, x2.x7 which clearly is not correct. Here is some code..

//Filter param is a list of filter parameters.
val q3 = q2.sortBy(resultSort(_, filterParams.sortTerms.head, filterParams.sortDirections.head))
    val direction = if(!filterParams.sortDirections.tail.isEmpty && filterParams.sortDirections.tail.head == "desc")
                         "desc"
                    else
                         "asc"
    val q4 = if (!filterParams.sortTerms.tail.isEmpty){
     q3.sortBy(resultSort(_, filterParams.sortTerms.tail.head, direction))
    }
    else q3

result sort is a method which will sort q2 query elements.

Is there something wrong here?  if I do sort chaining the order should be as it is i.e. in this case Ideally order by should be x2.x7, x23.x32 desc 

Thanks,
Pradeep



Stefan Zeiger

unread,
Feb 7, 2013, 6:27:41 AM2/7/13
to scala...@googlegroups.com
On 2013-02-06 23:31, Pradeep Kumar Mishra wrote:
I am trying to implement sorting over two fields lets say price, rating in order of asc, desc respectively. I see the results are not appropriate and after I print the statement I see the order is reversed i.e. I am geting statement ending with order by x23.x30 desc, x2.x7 which clearly is not correct. Here is some code..

//Filter param is a list of filter parameters.
val q3 = q2.sortBy(resultSort(_, filterParams.sortTerms.head, filterParams.sortDirections.head))
    val direction = if(!filterParams.sortDirections.tail.isEmpty && filterParams.sortDirections.tail.head == "desc")
                         "desc"
                    else
                         "asc"
    val q4 = if (!filterParams.sortTerms.tail.isEmpty){
     q3.sortBy(resultSort(_, filterParams.sortTerms.tail.head, direction))
    }
    else q3

I have no idea what your column names are supposed to be, so I'll assume that you expect .sortBy(_.a).sortBy(_.b) to generate code like "order by a, b". That would be wrong. sortBy(x => (x.a, x.b))  will do that but in .sortBy(_.a).sortBy(_.b) b wins. The sorting methods on Scala collections are stable, so Slick adds a as a secondary criterion to give you the same effect, making it "order by b, a". For non-stable sorting, a would be completely irrelevant, making it "order by b".

--
Stefan Zeiger
Typesafe - The software stack for applications that scale
Twitter: @StefanZeiger

Pradeep Kumar Mishra

unread,
Feb 7, 2013, 9:31:00 AM2/7/13
to scala...@googlegroups.com
Thanks Stefan.  Makes perfect sense to me.

Regards,
Pradeep
Reply all
Reply to author
Forward
0 new messages