異なるmapオブジェクトのlistソート

1813 ワード

// 
class ComparatorChannel implements Comparator{
    public int compare(Object arg0, Object arg1) {
        NewsChannel channel0=(NewsChannel)arg0;
        NewsChannel channel1=(NewsChannel)arg1;
        int flag=channel0.sortIndex.compareTo(channel1.sortIndex);
        if(flag==0){
            return channel0.srpId.compareTo(channel1.srpId);
        }else{
            return flag;
        }
    }
}
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 $ {
    public static void main(String[] args) {
        List<Map<String, String>> list = new ArrayList<Map<String, String>>();
 
        list.add(getData(0));
        list.add(getData(3));
        list.add(getData(05));
        list.add(getData(6));
        list.add(getData(2));
 
        System.out.println(" " + list);
 
        Collections.sort(list, new Comparator<Map<String, String>>() {
            public int compare(Map<String, String> o1, Map<String, String> o2) {
                return o1.get("countScore").compareTo(o2.get("countScore"));
            }
        });
 
        System.out.println(" " + list);
    }
 
    private static Map<String, String> getData(int num) {
        Map<String, String> map = new HashMap<String, String>();
        map.put("countScore", String.valueOf(num));
        return map;
    }
}