java中List按照指定字段排序工具类

简介:

文章标题:java中List按照指定字段排序工具类.

文章地址: http://blog.csdn.net/5iasp/article/details/17717179

 

包括如下几个类

 

1. 实体类

 

package com.newyear.wish;

/**
 * 实体类
 *
 */
public class Video {

	public Video(int id, String title, int hits) {
		super();
		this.id = id;
		this.title = title;
		this.hits = hits;
	}

	private int id;
	private String title;
	private int hits;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public int getHits() {
		return hits;
	}
	public void setHits(int hits) {
		this.hits = hits;
	}

}


2. list排序工具类

 

package com.newyear.wish;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 *  List按照指定字段排序工具类
 *
 * @param <T>
 */

public class ListSortUtil<T> {
	/**
	 * @param targetList 目标排序List
	 * @param sortField 排序字段(实体类属性名)
	 * @param sortMode 排序方式(asc or  desc)
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void sort(List<T> targetList, final String sortField, final String sortMode) {
	
		Collections.sort(targetList, new Comparator() {
			public int compare(Object obj1, Object obj2) { 
				int retVal = 0;
				try {
					//首字母转大写
					String newStr=sortField.substring(0, 1).toUpperCase()+sortField.replaceFirst("\\w",""); 
					String methodStr="get"+newStr;
					
					Method method1 = ((T)obj1).getClass().getMethod(methodStr, null);
					Method method2 = ((T)obj2).getClass().getMethod(methodStr, null);
					if (sortMode != null && "desc".equals(sortMode)) {
						retVal = method2.invoke(((T) obj2), null).toString().compareTo(method1.invoke(((T) obj1), null).toString()); // 倒序
					} else {
						retVal = method1.invoke(((T) obj1), null).toString().compareTo(method2.invoke(((T) obj2), null).toString()); // 正序
					}
				} catch (Exception e) {
					throw new RuntimeException();
				}
				return retVal;
			}
		});
	}
	
}


3. 测试类

package com.newyear.wish;

import java.util.ArrayList;
import java.util.List;

public class ListSortUtilTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		
		List<Video> targetList = new ArrayList<Video>();
		targetList.add(new Video(1,"title1",31));
		targetList.add(new Video(2,"title2",12));
		targetList.add(new Video(3,"title3",53));
		System.out.println("排序前: " + targetList);
		
		ListSortUtil<Video> sortList = new ListSortUtil<Video>();
		sortList.sort(targetList, "hits", "asc");
		System.out.println("排序后:" +targetList);

	}

}


 


测试执行结果:

排序前: [com.newyear.wish.Video@c17164, com.newyear.wish.Video@1fb8ee3, com.newyear.wish.Video@61de33]
排序后:[com.newyear.wish.Video@1fb8ee3, com.newyear.wish.Video@c17164, com.newyear.wish.Video@61de33]

 

目录
相关文章
|
21天前
|
Java
java8中List对象转另一个List对象
java8中List对象转另一个List对象
36 0
|
25天前
Mybatis+mysql动态分页查询数据案例——分页工具类(Page.java)
Mybatis+mysql动态分页查询数据案例——分页工具类(Page.java)
21 1
|
25天前
Mybatis+mysql动态分页查询数据案例——工具类(MybatisUtil.java)
Mybatis+mysql动态分页查询数据案例——工具类(MybatisUtil.java)
15 1
|
18天前
|
Java
Java使用List去重的四中方式
Java使用List去重的四中方式
16 6
|
21天前
|
JSON Java 网络安全
Java使用hutool工具类发送网络请求
Java使用hutool工具类发送网络请求
40 0
|
22天前
Cause: java.sql.SQLIntegrityConstraintViolationException: Column ‘id‘ in field list is ambiguous
Cause: java.sql.SQLIntegrityConstraintViolationException: Column ‘id‘ in field list is ambiguous
12 0
|
1月前
|
Java
JAVA——List中剔除空元素(null)的三种方法汇总
JAVA——List中剔除空元素(null)的三种方法汇总
|
1月前
|
安全 Java API
Java并发 - J.U.C并发容器类 list、set、queue
Queue API 阻塞是通过 condition 来实现的,可参考 Java 并发 - Lock 接口 ArrayBlockingQueue 阻塞 LinkedBlockingQueue 阻塞 ArrayQueue 非阻塞 LinkedQueue 非阻塞
|
1月前
|
Java
如何使用 Java 8 进行字符串排序?
【2月更文挑战第21天】
66 3
|
1月前
|
搜索推荐 算法 C++
list容器-排序案例讲解
list容器-排序案例讲解
9 0