Use findList() and not findPagingList() - that will return a List... and then on the list sort it yourself using your own comparator.
EBean preserve sort order from in clause
سؤال
I have a Database with documents which I index with Lucene - not all of the fields a relevant for search, so just a subset of them is actually indexed. A search gives me a list of document ids ordered by their score. I use this list to find Ebeans
PagingList<MyDoc> pagingList = Ebean.find(MyDoc.class)
.where().in("id", idList)
.findPagingList(pageSize);
I would like to preserve the sort order obviously. As far as I know, there's no build in way in EBean, to preserve the order and I couldn't find a sort function, which let's me implement a Comparator either.
Is there any way of doing this?
update
As said by Rob, I'm using a List<> and sort it myself.
PagingList<MyDoc> pagingList = Metadata.find(MyDoc.class)
.where().in("id", idList).findPagingList(PAGE_SIZE);
Page<MyDoc> pg = pagingList.getPage(page);
List<MyDoc> results = pg.getList();
Collections.sort(results, new Comparator<MyDoc>() {
@Override
public int compare(MyDoc o1, MyDoc o2) {
if (idList.indexOf(o1.id) > idList.indexOf(o2.id)) {
return 1;
}
else if (idList.indexOf(o1.id) < idList.indexOf(o2.id)) {
return -1;
}
else {
return 0;
}
}
});
المحلول
لا تنتمي إلى StackOverflow