Q:

Order list of objects by custom order initialized in other list

i have list a of objects which type is Person : 
class Person{
    int id
    String name
    String bestColor
}

def persons = [ new Person(1,'Abdennour','white'),
                new Person(2,'Ali','red'),
                new Person(3,'Hsen','white'),
                new Person(4,'Aicha','green') ]

and i have a list of color: 
def colors=['green','white','red']

i want to order persons list according to 3rd field(bestColor) . However, I do not want alphabetical order of colors , but rather I want the same order as colors  list.
that it means , the expected result is : 
def persons=[new Person(4,'Aicha','green')
,new Person(1,'Abdennour','white')
,new Person(3,'Hsen','white')
,new Person(2,'Ali','red')]

A:

So given:
@groovy.transform.Canonical
class Person{
    int id
    String name
    String bestColor
}

def persons = [ new Person( 1, 'Abdennour', 'white' ),
                new Person( 2, 'Ali', 'red' ),
                new Person( 3, 'Hsen', 'white' ),
                new Person( 4, 'Aicha', 'green' ) ]

def colors = [ 'green','white','red' ]

You can just do:
// Sort (mutating the persons list)
persons.sort { colors.indexOf( it.bestColor ) }

// check it's as expected
assert persons.id == [ 4, 1, 3, 2 ]

