参照http://jardot.iteye.com/blog/762349
定义一个实体UserInfo
package com.test; import java.util.Date; public class UserInfo { Integer userId; String userName; Date birthDate; Integer age; public Integer getUserId() { return userId; } public void setUserId(int userId) { this.userId = userId; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public Date getBirthDate() { return birthDate; } public void setBirthDate(Date birthDate) { this.birthDate = birthDate; } public Integer getAge() { return age; } public void setAge(int age) { this.age = age; } public UserInfo(Integer id, String name, Date date, Integer age) { super(); this.userId = id; this.userName = name; this.birthDate = date; this.age = age; } @Override public String toString() { return "UserInfo [userId=" + userId + ", userName=" + userName + ", birthDate=" + birthDate + ", age=" + age + "]"; } }
通用类:SortList
package com.test; 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<E>{ public void Sort(List<E> list, final String method, final String sort){ Collections.sort(list, new Comparator() { public int compare(Object a, Object b) { int ret = 0; try{ Method m1 = ((E)a).getClass().getMethod(method, null); Method m2 = ((E)b).getClass().getMethod(method, null); if(sort != null && "desc".equals(sort))//倒序 ret = m2.invoke(((E)b), null).toString().compareTo(m1.invoke(((E)a), null).toString()); else//正序 ret = m1.invoke(((E)a), null).toString().compareTo(m2.invoke(((E)b), null).toString()); }catch(NoSuchMethodException ne){ System.out.println(ne); }catch(IllegalAccessException ie){ System.out.println(ie); }catch(InvocationTargetException it){ System.out.println(it); } return ret; } }); } }
测试类:
package com.test; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.List; public class ListSort { public static void main(String[] args) throws Exception { List<UserInfo> list = new ArrayList<UserInfo>(); SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd"); formater.parse("1980-12-01"); list.add(new UserInfo(3, "b", formater.parse("1980-12-01"), 11)); list.add(new UserInfo(1, "c", formater.parse("1980-10-01"), 30)); list.add(new UserInfo(2, "a", formater.parse("1973-10-01"), 11)); System.out.println("-------原来序列-------------------"); for (UserInfo user : list) { System.out.println(user.toString()); } //调用排序通用类 SortList<UserInfo> sortList = new SortList<UserInfo>(); //按userId排序 sortList.Sort(list, "getUserId", "desc"); System.out.println("--------按userId倒序------------------"); for (UserInfo user : list) { System.out.println(user.toString()); } //按userName排序 sortList.Sort(list, "getUserName", null); System.out.println("---------按username排序-----------------"); for (UserInfo user : list) { System.out.println(user.toString()); } //按birthDate排序 sortList.Sort(list, "getBirthDate", null); System.out.println("---------按birthDate排序-----------------"); for (UserInfo user : list) { System.out.println(user.toString()); } //按birthDate排序 sortList.Sort(list, "getAge", null); System.out.println("---------按age排序-----------------"); for (UserInfo user : list) { System.out.println(user.toString()); } } }
相关推荐
List对象排序通用方法List对象排序通用方法List对象排序通用方法List对象排序通用方法
天才之作:通过泛型,对List中对象多属性排序,支持设置各属性排序方式(动态属性),看了之后绝对让你震撼!
LIST对象排序通用方法HIBERNATE的SET排序.pdf
分析LIST对象排序通用方法HIBERNATE的SET排序.pdf
LIST对象排序通用方法HIBERNATE的SET排序[整理].pdf
List对象排序通用方法[参考].pdf
主要介绍了java中List对象排序通用方法,涉及java针对List对象的操作技巧,需要的朋友可以参考下
NULL 博文链接:https://royal2xiaose.iteye.com/blog/1490176
对List集合中 对象属性排序,不需要在每个实体类中重写Sort方法,实现了代码的重用。非常好用的Util类
主要介绍了java实现List中对象排序的方法,涉及Java中的遍历与对象操作技巧,具有一定参考借鉴价值,需要的朋友可以参考下
在开发中,我们可能会遇到,我们会根据不同情况对一个List集合进行排序操作,为了方便List集合根据对象中的某一个属性进行排序,开发了此工具类:
java版本List中对象多属性排序.rar
导入jar 包 调用 String[] arr1 = new String[1]; ... 参数1 排序的集合 参数2 排序的字段(与定义字段一致) 可多个 参数3 排序方式(asc desc) 暂时只支持String 和int的排序 可能有些BUG 敬请谅解
arraylist 对象中的某个值排序,并非单出的数组排序
// 构造多个person对象为排序提供数据 Person person = new Person(); person.setName("wtt"); person.setSex("男"); person.setAge(24); Person person1 = new Person(); person1.setName("wjj"); person1....
对集合中的对象进行排序,例如:List中存放了User对象,则可根据User对象的某一个属性进行顺序or倒序的排序