Reputation: 3
I need to sort a list of Dynamic object with a list of dynamic search criteria. So I have a method which accepts
sort(List<SortCriterion> sortCriteria, List<T> searchList){
}
Now If I have an object list of TradeOperator which looks like this. I can send other object Like CarOperator in the sort method.
public class TradeOperator implements SearchResult{
private String name;
private String address;
private String status;
private Long regNum;
private Double price;
}
The SortCriterion code looks like this.
public class SortCriterion{
protected String field;
protected String direction;
}
where the field could be TradeOperator.name and the direction = ASC or DESC. Now i can pass the list of SortCriterion with multiple fields and direction.
Here is what I did. I Collected all the fields which are ASC and all the field which are DESC in a MAP sortValues with Key = ASC and DESC and Values are list of the Field. Then Sort it in ASC and then in reverse order.
sort(List<SortCriterion> sortCriteria, List<T> searchList){
Map<String, Set<String>> sortValues = new HashMap<>();
populateSortedValues(sortCriteria, "ASC", sortValues);//group fields on ASC or DESC as key = ASC and Value = List of Fields
populateSortedValues(sortCriteria, "DESC", sortValues);
if (null != sortValues.get(Constants.ASC_ORDER))
{
for (String ascendingOrder :
sortValues.get(Constants.ASC_ORDER))
{
Collections.sort(list, new SearchResultComparator(ascendingOrder));
}
}
if (null != sortValues.get(Constants.DESC_ORDER))
{
for (String descOrder : sortValues.get(Constants.DESC_ORDER))
{
Collections.sort(list, new
SearchResultComparator(descOrder).reversed());
}
}
}
Here is the SearchResultComparator class.
public class SearchResultComparator implements Comparator<Object> {
private String getter;
public SearchResultComparator(String field) {
this.getter = "get" + field.substring(0, 1).toUpperCase() + field.substring(1);
}
@SuppressWarnings("unchecked")
public int compare(Object o1, Object o2) {
try {
if (o1 != null && o2 != null) {
o1 = o1.getClass().getMethod(getter, new Class[0]).invoke(o1, new Object[0]);
o2 = o2.getClass().getMethod(getter, new Class[0]).invoke(o2, new Object[0]);
if(isDouble(o1.toString()) && isDouble(o2.toString())){
Double d1 = Double.parseDouble(o1.toString());
Double d2 = Double.parseDouble(o2.toString());
return (d1 == null) ? -1 : ((d2 == null) ? 1 : ((Comparable<Double>) d1).compareTo(d2));
}
}
} catch (Exception e) {
throw new SystemException("Cannot compare " + o1 + " with " + o2 + " on " + getter, e);
}
return (o1 == null) ? -1 : ((o2 == null) ? 1 : ((Comparable<Object>) o1).compareTo(o2));
}
public boolean isDouble(String value)
{
try
{
Double.parseDouble(value);
return true;
}
catch (NumberFormatException e)
{
return false;
}
}
}
Its Sorting in One way. if I sort a field in ASC then another field in DESC the ASC sort is getting lost. Please help. I am new to Java.
Upvotes: 0
Views: 2005
Reputation: 58
You don't need SearchResultComparator or segregate ASC and DESC. Use this technique for chained sorting an any order.
sort(List<SortCriterion> sortCriteria, List<T> list) {
Collections.sort(list, new Comparator<T>() {
public int compare(T one, T two) {
CompareToBuilder compToBuild = new CompareToBuilder();
sortCriteria.stream().forEachOrdered(sc -> {
String fieldName = sc.getField();
String direction = sc.getDirection();
String fv1 = getFieldValue(fieldName, one);
String fv2 = getFieldValue(fieldName, two);
if(direction.equals("ASC")) {
compToBuild.append(fv1, fv2);
}
if(direction.equals("DESC")) {
compToBuild.append(fv2, fv1);
}
});
return compToBuild.toComparison();
}
});
}
Get the field value using reflection
private String getFieldValue(String fieldName, T object) {
Field field;
try {
field = object.getClass().getDeclaredField(fieldName);
} catch (NoSuchFieldException e) {
logger.error(e);
return null;// or ""
}
field.setAccessible(true);
try {
return (String) field.get(object);
} catch (IllegalAccessException e) {
logger.error(e);
return null;// or ""
}
}
Upvotes: 1