Merlin
Merlin

Reputation: 4917

Grails giving sorted list to findAllByIdInList

I dont want the result re-sorted.

Example:

def ids = [7, 9, 5, 6, 12, 2, 10, 1, 42, 13]
result = Project.findAllByIdInList(ids).collect{
    projectUtil.createProjectDTO(it)
}

Then if I get the Ids of object created they are sorted this way

result = [1, 2, 5, 6, 7, 9, 10, 12, 13, 42]

I'd like to keep the same order is there a way to achieve that ?

Thanks in advance and sorry for my bad english :)

Upvotes: 0

Views: 249

Answers (2)

user800014
user800014

Reputation:

Another option is withCriteria

result = Project.withCriteria {
  'in'('id', ids)
}.collect { projectUtil.createProjectDTO(it) }

Upvotes: 1

cawka
cawka

Reputation: 437

You could try using getAll instead of findAllByIdInList:

def ids = [7, 9, 5, 6, 12, 2, 10, 1, 42, 13]
result = Project.getAll(ids).collect {
    projectUtil.createProjectDTO(it)
}

I just tested it with a similar example in an integration test and it returned the instances in the same order as declared in the list of IDs.

Upvotes: 1

Related Questions