singalen: (Default)

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}
    }
}

Tags: