对象排序
来源:互联网 发布:压实度的数据 编辑:程序博客网 时间:2024/06/03 17:48
排序实现类:
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); } }}
1 0
- 排序对象
- 对象排序
- 对象排序
- 对象排序
- 对象排序
- 对象排序
- 对象排序
- 对象排序
- 对象排序
- 数组排序与对象排序
- java对象排序、中文排序、list排序
- java 对象排序
- java 对象排序
- 对象属性的排序
- Java集合对象排序
- java对象排序
- java对象排序
- List 对象属性排序
- css3直角梯形
- linux学习教程(一)安装篇
- 黑马程序员——OC基础04—block、protocol
- IOS开发核心语言0bjective C
- Arduino系列教程之 – PWM的秘密
- 对象排序
- Android中ImageView的图片宽度设置
- iOS 求两经纬度中心点,并返回缩放级别,可以参考
- Android ListView嵌套checkbox点击事件
- HTTP详解(1)-工作原理
- Mushroom User's Manual
- 通过ZBOSS实现2530/2531 uart/usb wireshark 抓RF包(sniffer)
- C#数据库类库及操作数据基础
- merge into 当using后的select结果为空时,不能执行update和insert