Skip to content

Instantly share code, notes, and snippets.

@faogustavo
Created August 4, 2016 17:26
Show Gist options
  • Save faogustavo/be504f596cf556da48ef5ce1120742fc to your computer and use it in GitHub Desktop.
Save faogustavo/be504f596cf556da48ef5ce1120742fc to your computer and use it in GitHub Desktop.
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class SortedList<T> extends ArrayList<T> {
public enum SortOrder {ASC, DESC}
private final Class<T> typeParameterClass;
public SortedList(Class<T> typeParameterClass) {
this.typeParameterClass = typeParameterClass;
}
public void sortByAttribute(String attributeName) throws NoSuchMethodException {
this.sortByFunction(attributeName, SortOrder.ASC);
}
public void sortByAttribute(String attributeName, SortOrder order) throws NoSuchFieldException, IllegalAccessException {
Field declaredAttribute = typeParameterClass.getDeclaredField(attributeName);
Class<?> returnType = declaredAttribute.getType();
boolean oldAcessibility = declaredAttribute.isAccessible();
declaredAttribute.setAccessible(true);
Collections.sort(this, new Comparator<T>() {
@Override
public int compare(T o1, T o2) {
int returnValue = 0;
try {
returnValue = compareTo(declaredAttribute.get(o1), declaredAttribute.get(o2), returnType);
} catch (Exception e) {}
if (order == SortOrder.DESC) {
returnValue *= -1;
}
return returnValue;
}
});
declaredAttribute.setAccessible(oldAcessibility);
}
public void sortByFunction(String functionName, Object... args) throws NoSuchMethodException {
this.sortByFunction(functionName, SortOrder.ASC, args);
}
public void sortByFunction(String functionName, SortOrder order, Object... args) throws NoSuchMethodException {
Method method = typeParameterClass.getMethod(functionName);
Class<?> returnType = method.getReturnType();
boolean oldAcessibility = method.isAccessible();
method.setAccessible(true);
Collections.sort(this, new Comparator<T>() {
@Override
public int compare(T o1, T o2) {
int returnValue = 0;
try {
returnValue = compareTo(method.invoke(o1, args), method.invoke(o2, args), returnType);
} catch (Exception e) {}
if (order == SortOrder.DESC) {
returnValue *= -1;
}
return returnValue;
}
});
method.setAccessible(oldAcessibility);
}
protected int compareTo(Object o1, Object o2, Class objType) {
if (objType.equals(String.class)) {
String o1Val = String.valueOf(o1);
String o2Val = String.valueOf(o2);
return o1Val.compareTo(o2Val);
} else if (objType.equals(Number.class)) {
Number n1 = (Number) o1;
Number n2 = (Number) o2;
if (n1.doubleValue() > n2.doubleValue()) {
return -1;
} else if (n1.doubleValue() < n2.doubleValue()) {
return 1;
} else {
return 0;
}
} else if (objType.equals(int.class)
|| objType.equals(double.class) || objType.equals(float.class)
|| objType.equals(char.class) || objType.equals(long.class) || objType.equals(short.class)) {
Double d1 = Double.parseDouble(String.valueOf(o1));
Double d2 = Double.parseDouble(String.valueOf(o2));
return d1.compareTo(d2);
}
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment