singalen: (Default)
singalen ([personal profile] singalen) wrote2011-02-15 05:23 pm
Entry tags:

Groovy Comparator for class field names

Originally published at code name. You can comment here or there.

Google Buzz

I wonder why this was not in JDK/Groovy Collections.

Just put together a Comparator for StackOverflow question – a Groovy Comparator that would sort class field names in the declaration order. Feel free to use:

class PropComparator implements Comparator {
    private Class clazz
    PropComparator(Class clazz) { this.clazz = clazz }

    int compare(Object o1, Object o2) {
        clazz.declaredFields.findIndexOf{it.name == o1}
          - clazz.declaredFields.findIndexOf{it.name == o2}
    }
}


Post a comment in response:

This account has disabled anonymous posting.
If you don't have an account you can create one now.
HTML doesn't work in the subject.
More info about formatting