Java Collections Sort 排序

  Java Collections sort方法集合排序:
package com.anyfo.invocation.test;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import com.anyfo.invocation.admin.util.MapComparator;

public class TestCollectionsSort {

	public static void main(String[] args) {
		
		List<Map<String, Object>> list = new LinkedList<Map<String, Object>>();
		Collections.sort(list, MapComparator.getInstance());

	}

}
package com.anyfo.invocation.admin.util;

import java.util.Comparator;
import java.util.Map;

public class MapComparator implements Comparator<Map<String, Object>> {

	private static final Comparator<Map<String, Object>> instance = new MapComparator();
	
	public static Comparator<Map<String, Object>> getInstance() {
		return instance;
	}
	
	@SuppressWarnings("unchecked")
	public int compare(Map o1, Map o2) {
		if (o1 == null) {
			return -1;
		} else if (o2 == null) {
			return 1;
		} else {
			return ((String)o1.get("label")).compareTo((String)o2.get("label"));
		}
	}

}

 

你可能感兴趣的:(java)