Bigdata, Hadoop ecosystem, Semantic IoT등의 프로젝트를 진행중에 습득한 내용을 정리하는 곳입니다.
필요한 분을 위해서 공개하고 있습니다.
문의사항은 gooper@gooper.com로 메일을
보내주세요.
1. 정렬 수행
// data를 담고 있는 List변수
List<Map<String, String>> list = new ArrayList<Map<String, String>>();
// List에 담기는 Map변수
HashMap<String,String> row = new HashMap<String, String>;
// 정렬실행
Collections.sort(list, new MapStringComparator("rest_type"));
Collections.sort(list, new MapStringComparator("corner_id"));
Collections.sort(list, new MapFloatComparator("cnt"));
2. 정렬 클래스(문자열)
class MapStringComparator implements Comparator<Map<String, String>> { private final String key; public MapStringComparator(String key) { this.key = key; } @Override public int compare(Map<String, String> first, Map<String, String> second) { String firstValue =first.get(key); String secondValue = second.get(key); // 내림차순 정렬 return firstValue.compareTo(secondValue); } }
3. 정렬 클래스(숫자)
class MapFloatComparator implements Comparator<Map<String, String>> { private final String key; public MapFloatComparator(String key) { this.key = key; } @Override public int compare(Map<String, String> first, Map<String, String> second) { float firstValue = Float.valueOf(first.get(key)); float secondValue = Float.valueOf(second.get(key)); // 내림차순 정렬 if (firstValue > secondValue) { return -1; } else if (firstValue < secondValue) { return 1; } else /* if (firstValue == secondValue) */ { return 0; } } }