2

I want to keep the order of my first list

def myList = [444,1111,33333,22222]

But when I use findAllBy the order is changed

def myList2 = myDomain.findAllByRmIdInList(myList)

=> out : [1111, 22222, 33333, 444]

There is a way to desactivate order by default?

Thanks

2 Answers 2

3

If you want to keep the order of your list, you can use the dynamic finder 'getAll()' (here is the documentation)

Retrieves a List of instances of the domain class for the specified ids, ordered by the original ids list. If some of the provided ids are null or there are no instances with these ids, the resulting List will have null values in those positions.

So you could try the next code:

def myList2 = myDomain.getAll(myList)

UPDATED AFTER COMMENTS

You can use a Comparator for that. It is a little bit more tricky, but it should work. Below you'll find an 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 } ] as Comparator
def myList2 = myDomain.findAllByRmIdInList(myList)
def results = myList2.sort(mc) 

results.each() {
    log.info(it.rmId)
}

Hope that helps.

4
  • As specified in the documentation I need to have ids, but in my case I don't have them. I can get ids for all my elements, one by one, but I want to avoid that (I will do that if I don't have the choice).
    – Jils
    Sep 22, 2014 at 11:13
  • Hi Jils. Sorry for the first answer, I didn't see the 'rm' before 'id' in your dynamic finder. I just updated my answer with a comparator. It should work.
    – Abincepto
    Sep 22, 2014 at 12:06
  • Thanks its work. Can I have some explanation of your seconde line?
    – Jils
    Sep 22, 2014 at 12:52
  • You're welcome. The second line is a "Comparator". It is a new rule to sort your list. 'a' and 'b' are two elements of your findAll(), so they belong to 'myDomain'. If a.rmId == b.rmId, then I suppose that a == b (-> 0). If the position of a.rmId in myList is lower than the position of b.rmId in MyList, then 'a' has to appear before 'b' in the result (-> -1). If the position of a.rmId in myList is greater than the position of b.rmId in myList, then 'a' will appear after 'b' (-> 1).
    – Abincepto
    Sep 22, 2014 at 13:25
1

You should use

def myList2 = MyDomain.findAllByRmIdInList(myList, [sort:"pmId"])

if you want you can also pass order either "desc" or "asc"

Note: finders works on Domain not on controllers.

7
  • I added [sort:"prop"] and I have the following error: could not resolve property: prop of: app.myDomain.
    – Jils
    Sep 22, 2014 at 10:01
  • In place of "prop" write your domain property on which you want to sort.
    – ABC
    Sep 22, 2014 at 10:10
  • As I wrote I don't want to do a sort, I want to keep the order of my first list. Maybe my question is not clear...
    – Jils
    Sep 22, 2014 at 10:16
  • Did you have overridden your toString() ?
    – ABC
    Sep 22, 2014 at 10:40
  • toString() in the Domain? yes I did.
    – Jils
    Sep 22, 2014 at 10:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.