1. 程式人生 > >java list 排序

java list 排序

package cn.com.itrus.raapi;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ListTest {

	public static void main(String[] args) {
		 
        List<Map<Object, Object>> list = new ArrayList<Map<Object, Object>>();
 
        Map<Object, Object> map = new HashMap<Object, Object>();
 
        Map<Object, Object> map1 = new HashMap<Object, Object>();
        Map<Object, Object> map2 = new HashMap<Object, Object>();
        Map<Object, Object> map3 = new HashMap<Object, Object>();
 
        map.put("number", "2.57");
        map1.put("number", "3");
        map2.put("number", "2.99");
        map3.put("number", "2.78");
        list.add(map);
        list.add(map1);
        list.add(map2);
        list.add(map3);
 
        Collections.sort(list, new Comparator<Map<Object, Object>>() {
 
            public int compare(Map<Object, Object> o1, Map<Object, Object> o2) {
 
                int map1value = (int)Double.parseDouble((String) o1.get("number"))*100 ;
                int map2value = (int)Double.parseDouble((String) o2.get("number"))*100;
                return map1value - map2value;
            }
        });
        System.out.println(list);
    }

}