Cloudera CDH/CDP 및 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;
}
}
}