[[Javaメモ]] > Mapリストをソートする
* JavaでMapのリストをソートする [#h6aec2b9]
#setlinebreak(on);
#mycode(){{
package test;
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 TestSort {
public static void main(String[] args) {
TestSort obj = new TestSort();
obj.execute();
}
public void execute(){
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
{
Map<String, Object> rec = new HashMap<String, Object>();
rec.put("COLNAME", "TEST2");
rec.put("VALUE", "BBBBB");
list.add(rec);
}
{
Map<String, Object> rec = new HashMap<String, Object>();
rec.put("COLNAME", "TEST2");
rec.put("VALUE", "CCCCCCCCC");
list.add(rec);
}
{
Map<String, Object> rec = new HashMap<String, Object>();
rec.put("COLNAME", "TEST1");
rec.put("VALUE", "AAAAAA");
list.add(rec);
}
Collections.sort(list, new Comparator<Map<String, Object>>(){
@Override
public int compare(Map<String, Object> rec1, Map<String, Object> rec2) {
String colName1 = (String)rec1.get("COLNAME");
String colName2 = (String)rec2.get("COLNAME");
return colName1.compareTo(colName2);
}
});
System.out.println(list);
}
}
}}