grails: keep list order when using findAllBy -
i want keep order of first list
def mylist = [444,1111,33333,22222]
but when use findallby
order changed
def mylist2 = mydomain.findallbyrmidinlist(mylist)
=> out : [1111, 22222, 33333, 444]
there way desactivate order default?
thanks
if want keep order of list, can use dynamic finder 'getall()' (here documentation)
retrieves list of instances of domain class specified ids, ordered original ids list. if of provided ids null or there no instances these ids, resulting list have null values in positions.
so try next code:
def mylist2 = mydomain.getall(mylist)
updated after comments
you can use comparator that. little bit more tricky, should work. below you'll find example:
def mylist = [444,1111,33333,22222] def mc = [compare: { a,b -> a.rmid == b.rmid ? 0 : mylist.indexof(a.rmid) < mylist.indexof(b.rmid) ? -1 : 1 } ] comparator def mylist2 = mydomain.findallbyrmidinlist(mylist) def results = mylist2.sort(mc) results.each() { log.info(it.rmid) }
hope helps.
Comments
Post a Comment