List 对象排序
来源:互联网 发布:c语言音乐播放器 编辑:程序博客网 时间:2024/06/07 23:20
一般对list排序可以使用Collections.sort(list),但如果list中包含的是对象,这种方法是行不通的。
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> {@SuppressWarnings("unchecked")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;}});}}测试类
import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Date;import java.util.List;public class TestSortList {public static void main(String[] args) throws Exception {List<UserInfo> list = new ArrayList<UserInfo>();SimpleDateFormat formater = new SimpleDateFormat("yyyy-MM-dd");list.add(new UserInfo(3, "b", formater.parse("1980-12-01")));list.add(new UserInfo(1, "c", formater.parse("1980-10-01")));list.add(new UserInfo(2, "a", formater.parse("1973-10-01")));System.out.println("-------The Original List-------------------");for (UserInfo user : list) {System.out.println(user.toString());}// 调用排序通用类SortList<UserInfo> sortList = new SortList<UserInfo>();// 按userId降序排序sortList.Sort(list, "getUserId", "desc");System.out.println("--------Desc Ordered by userId------------------");for (UserInfo user : list) {System.out.println(user.toString());}}}
package com.sun.demo;import java.util.Date;public class UserInfo {public int userId;public String username;public Date BirthDate;public UserInfo(int userId, String username, Date birthDate) {super();this.userId = userId;this.username = username;BirthDate = birthDate;}public int 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) {BirthDate = birthDate;}}0 0
- List 对象属性排序
- list 集合对象 排序
- list内对象排序
- List 对象排序
- List对象排序
- list中对象排序
- java List 对象排序
- List对象排序
- C# list对象排序
- List对象排序
- Java list 对象 排序
- java对象排序、中文排序、list排序
- 对List中对象排序
- 对LIST的对象排序
- List对象排序通用方法
- list 对象排序的类
- List对象排序通用方法
- List对象排序通用方法
- 【self-taught learning】学习论文以及代用代码
- 原生JavaScript技巧大收集
- PHP调试
- DML、DDL、DCL的含义
- C++匿名命名空间
- List 对象排序
- poj_1416Shredding Company
- android很有用的一个伙伴- Intent
- Eclipse3.2下配置ORBStudio插件的corba开发环境
- Starling 处理屏幕尺寸
- [多图]Xbox One Kinect内部芯片大揭秘
- android网络开源框架volley(二)——自定义你的请求对象
- windows net-snmp安装配置
- poj3686 km算法 巧妙的构图