排序实现类:
package test
import java.lang.reflect.Field
import java.lang.reflect.InvocationTargetException
import java.lang.reflect.Method
import java.util.Collections
import java.util.Comparator
import java.util.List
public class SortList<T> {
public void Sort(List<T> list, final String sortName, final String sort) {
Collections.sort(list, new Comparator<T>() {
@Override
public int compare(T o1, T o2) {
int ret = 0
try {
String method = "get" + sortName.substring(0, 1).toUpperCase() + sortName.substring(1)
Method m1 = o1.getClass().getMethod(method, null)
Method m2 = o1.getClass().getMethod(method, null)
// 降序排
if (sort != null && SortEnum.SORT_DESC.getValue().equals(sort)) {
ret = m2.invoke(o2, null).toString().compareTo(m1.invoke(o1, null).toString())
}
// 升序排
else {
ret = m1.invoke(o1, null).toString().compareTo(m2.invoke(o2, null).toString())
}
}
catch (NoSuchMethodException e) {
e.printStackTrace()
}
catch (SecurityException e) {
e.printStackTrace()
}
catch (IllegalAccessException e) {
e.printStackTrace()
}
catch (IllegalArgumentException e) {
e.printStackTrace()
}
catch (InvocationTargetException e) {
e.printStackTrace()
}
return ret
}
})
}
}
Employee类
package test;
import java.util.Date;
public class Employee {
private Integer name;
private String clazz;
private Integer year;
private Integer month;
private Integer day;
private double salary;
public Employee() {
};
public Employee(Integer name, String clazz, Integer year, Integer month, Integer day,
double salary) {
super();
this.name = name;
this.clazz = clazz;
this.year = year;
this.month = month;
this.day = day;
this.salary = salary;
}
/**
* @return name
*/
public Integer getName() {
return name;
}
/**
* @return year
*/
public Integer getYear() {
return year;
}
/**
* @return month
*/
public Integer getMonth() {
return month;
}
/**
* @return day
*/
public Integer getDay() {
return day;
}
/**
* @return salary
*/
public double getSalary() {
return salary;
}
/**
* @param name
* set name
*/
public void setName(Integer name) {
this.name = name;
}
/**
* @param year
* set year
*/
public void setYear(Integer year) {
this.year = year;
}
/**
* @param month
* set month
*/
public void setMonth(Integer month) {
this.month = month;
}
/**
* @param day
* set day
*/
public void setDay(Integer day) {
this.day = day;
}
/**
* @param salary
* set salary
*/
public void setSalary(double salary) {
this.salary = salary;
}
/**
* @return clazz
*/
public String getClazz() {
return clazz;
}
/**
* @param clazz
* set clazz
*/
public void setClazz(String clazz) {
this.clazz = clazz;
}
@Override
public String toString() {
return "Employee [name=" + name + ", clazz=" + clazz + ", year=" + year + ", month=" + month
+ ", day=" + day + ", salary=" + salary + "]";
}
}
枚举
package test;
/**
* 排序类型枚举
*
* @author user
*/
public enum SortEnum {
/**
* 降序
**/
SORT_DESC("desc"),
/**
* 升序
*/
SORT_ASC("asc");
private String value;
SortEnum(String value) {
this.value=value;
}
/**
* @return value
*/
public String getValue() {
return value;
}
}
测试类
package test
import java.util.ArrayList
import java.util.Calendar
import java.util.Collections
import java.util.Date
import java.util.GregorianCalendar
import java.util.HashMap
import java.util.Iterator
import java.util.List
import java.util.Map
public class ComparatorTest {
public static void main(String[] args) {
Employee staff11 = new Employee(11, "1group", 1200, 1990, 12, 25)
Employee staff12 = new Employee(12, "1group", 1300, 1991, 12, 26)
Employee staff13 = new Employee(13, "1group", 1400, 1992, 12, 27)
Employee staff21 = new Employee(21, "2group", 2200, 1993, 12, 25)
Employee staff22 = new Employee(22, "2group", 2300, 1994, 12, 26)
Employee staff23 = new Employee(23, "2group", 2400, 1995, 12, 27)
Employee staff31 = new Employee(31, "3group", 3200, 1996, 12, 25)
Employee staff32 = new Employee(32, "3group", 3300, 1997, 12, 26)
Employee staff33 = new Employee(33, "3group", 3400, 1998, 12, 27)
List<Employee> staffs = new ArrayList<Employee>()
staffs.add(staff11)
staffs.add(staff12)
staffs.add(staff13)
staffs.add(staff21)
staffs.add(staff22)
staffs.add(staff23)
staffs.add(staff31)
staffs.add(staff32)
staffs.add(staff33)
SortList<Employee> sortList = new SortList<Employee>()
sortList.Sort(staffs, "clazz", SortEnum.SORT_DESC.getValue())
for (Employee employee : staffs) {
System.out.println(employee)
}
}
}