import java.lang.reflect.Field; import java.math.BigDecimal; import java.util.Collections; import java.util.Comparator; import java.util.List; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; import com.google.common.collect.Lists; @Service public class ListUtil { private static final String TRUE = "true"; @SuppressWarnings({ "rawtypes", "unchecked" }) public static List extractionProperty(List list, String fieldName) { List rList = Lists.newArrayList(); try { for (int i = 0; i < list.size(); i++) { Object obj = list.get(i); Field field[] = list.get(i).getClass().getDeclaredFields(); for (Field f : field) { f.setAccessible(true); if (fieldName.equals(f.getName())) { rList.add(f.get(obj)); break; } } } if (rList.isEmpty()) { return null; } } catch (Exception e) { e.printStackTrace(); return null; } return rList; } @SuppressWarnings({ "rawtypes", "unchecked" }) public static void sort(List list, String fieldName, SortType sortType, FieldType fieldType) { final String FIELD_NAME = fieldName; final int SORT_TYPE = sortType.getValue(); final int FIELD_TYPE = fieldType.getValue(); Collections.sort(list, new Comparator() { public int compare(Object arg0, Object arg1) { try { Object object0 = (Object) arg0; Object object1 = (Object) arg1; Field field0[] = object0.getClass().getDeclaredFields(); Field field1[] = object1.getClass().getDeclaredFields(); String sVal0 = ""; String sVal1 = ""; BigDecimal bVal0 = BigDecimal.ZERO; BigDecimal bVal1 = BigDecimal.ZERO; int boVal0 = 0; int boVal1 = 0; for (Field f0 : field0) { f0.setAccessible(true); if (FIELD_NAME.equals(f0.getName())) { if (FieldType.CHARACTER.getValue() == FIELD_TYPE) { sVal0 = String.valueOf(f0.get(object0)); } else if (FieldType.DIGITAL.getValue() == FIELD_TYPE) { bVal0 = new BigDecimal(String.valueOf(f0.get(object0))); } else { if (TRUE.equals(String.valueOf(f0.get(object0)))) { boVal0 = 1; } else { boVal0 = 0; } } break; } } for (Field f1 : field1) { f1.setAccessible(true); if (FIELD_NAME.equals(f1.getName())) { if (FieldType.CHARACTER.getValue() == FIELD_TYPE) { sVal1 = String.valueOf(f1.get(object1)); } else if (FieldType.DIGITAL.getValue() == FIELD_TYPE) { bVal1 = new BigDecimal(String.valueOf(f1.get(object1))); } else { if (TRUE.equals(String.valueOf(f1.get(object1)))) { boVal1 = 1; } else { boVal1 = 0; } } break; } } if (SortType.DESC.getValue() == SORT_TYPE) { if (FieldType.CHARACTER.getValue() == FIELD_TYPE) { return sVal1.compareTo(sVal0); } else if (FieldType.DIGITAL.getValue() == FIELD_TYPE) { return bVal1.compareTo(bVal0); } else { return boVal1 < boVal0 ? 1 : -1; } } else { if (FieldType.CHARACTER.getValue() == FIELD_TYPE) { return sVal0.compareTo(sVal1); } else if (FieldType.DIGITAL.getValue() == FIELD_TYPE) { return bVal0.compareTo(bVal1); } else { return boVal0 < boVal1 ? 1 : -1; } } } catch (Exception e) { e.printStackTrace(); return 0; } } }); } } public enum FieldType { DIGITAL(0), CHARACTER(1), BOOLEAN(2); private int value; private FieldType(int value) { this.value = value; } public int getValue() { return value; } public void setValue(int value) { this.value = value; } } public enum SortType { ASC("ASC", 1), DESC("DESC", 2); private String name; private int value; private SortType(String name, int value) { this.name = name; this.value = value; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getValue() { return value; } public void setValue(int value) { this.value = value; } }
时间: 2024-11-02 23:28:22