private <T extends Comparable<T>> int compare(Storable s1, Storable s2, String fieldName, Boolean desc) { try { Comparable<T> field1 = ReflectionHelper.<T>invokeGetter(fieldName, s1); T field2 = ReflectionHelper.invokeGetter(fieldName, s2); int res; if (field1 instanceof String && field2 instanceof String) { res = ((String) field1).compareToIgnoreCase((String) field2); } else { res = field1.compareTo(field2); } return (desc != null && desc) ? -res : res; } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException ex) { LOG.error("Got exception trying to get value for " + fieldName, ex); throw new RuntimeException(ex); } }