View Full Version : spring-data mongodb and sorting with multiple fields
jelinden
Apr 5th, 2011, 12:34 PM
I have two fields I want to sort with
(order by clicks descending, date descending)
query.sort().on("clicks", Order.DESCENDING);
query.sort().on("date", Order.DESCENDING);
With this it won't work, date is always messing the whole sort. Both work separately, but not together. I might be doing this the wrong way, but didn't come up with anything else.
Is anyone else doing something like this?
trisberg
Apr 6th, 2011, 07:29 PM
Have you tried:
query.sort().on("clicks", Order.DESCENDING).on("date", Order.DESCENDING);
If so what is the result?
jelinden
Apr 6th, 2011, 11:46 PM
Yes, I've tried, the result is the same.
For some reason the sort with date is always the number one sort.
Behauviour of these are exactly the same:
query.sort().on("date", Order.DESCENDING).on("clicks", Order.DESCENDING);
query.sort().on("clicks", Order.DESCENDING).on("date", Order.DESCENDING);
jelinden
Apr 9th, 2011, 08:08 AM
Really strange, made a simplified test to reproduce this, but it worked correctly.
As a last resort I added an index:
mongoTemplate.ensureIndex("collection", new Index("clicks", Order.DESCENDING).on("date", Order.DESCENDING));
this didn't help straight away. But by accident I removed .on("date", Order.DESCENDING); from the actual search sort ... and ... it works now.
blakgeek
Jun 19th, 2011, 07:52 PM
I don't know if you ever solved this or not. But in case someone else runs into it, I solved it by modifying the Sort class to use a LinkedHashMap so that the order would be maintained. I also entered a jira ticket so hopefully it will be fixed in the next version.
Oliver Gierke
Jul 6th, 2011, 08:42 AM
Fixed and will be available in tonights snapshot: https://jira.springsource.org/browse/DATADOC-177
Powered by vBulletin® Version 4.2.1 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.