用Collection.sort对List做排序

语法:

Collection.sort(List,Comparator);

List为需要排序的List对象,另外一个是比较器


比较器:

此处示例为List中存放的对象为Map。

public class KmComparator implements Comparator {

	public String column;
	public String type;
	
	public KmComparator(String column, String type) {
		super();
		this.column = column;
		this.type = type;
	}
	
	@Override
	public int compare(Object o1, Object o2) {
		Map<String,String> map1=(Map<String,String>) o1;
		Map<String,String> map2=(Map<String,String>) o2;
		if(type.equals("up")){
			return new Double(map2.get(column)).compareTo(new Double(map1.get(column)));
		}else{
			return new Double(map1.get(column)).compareTo(new Double(map2.get(column)));
		}
		
	}
	
}
引用如下:

Collections.sort(lists,new KmComparator(column,"up"));
此处up是升序排序。


你可能感兴趣的:(用Collection.sort对List做排序)