일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
- 네트워크
- profile
- import
- plugin
- ssh
- Quartz
- VirtualBox
- grep
- 줄바꿈 문자
- Eclipse
- web.xml
- bash
- netsh
- Mac
- lsof
- resource
- JavaScript
- context
- xargs
- vscode
- maVen
- Windows
- 단축키
- port
- Source
- find
- tomcat
- GIT
- Windows 10
- IntelliJ
- Today
- Total
develog
Collection List<Map> sort 본문
public class CollectionUtil {
public static void sort(List<Map<String, Object>> list, String keyNm) {
sort(list, keyNm, false);
}
public static void sort(List<Map<String, Object>> list, String keyNm, boolean descOrder) {
Comparator<Map<String, Object>> comparator = new ListMapComparator(keyNm);
Collections.sort(list, comparator);
if (descOrder) {
Collections.reverse(list);
}
}
}
class ListMapComparator implements Comparator<Map<String, Object>> {
private String keyNm;
public ListMapComparator(String keyNm) {
this.keyNm = keyNm;
}
public int compare(Map<String, Object> map1, Map<String, Object> map2) {
String value1 = String.valueOf(map1.get(keyNm));
String value2 = String.valueOf(map2.get(keyNm));
return value1.compareTo(value2);
}
}
------------------------------------------------------------------------------------------
List<Map<String, Object>> list = new ArrayList<HashMap<String, Object>>();
CollectionUtil .sort(list, "REG_TS"); // 오름차순
CollectionUtil .sort(list, "REG_TS", true); // 내림차순
'Dev > Java' 카테고리의 다른 글
정규표현식 #1 (0) | 2013.05.26 |
---|---|
[java] html tag 제거 (0) | 2013.05.26 |
Web scraper open source (0) | 2013.05.16 |
Collection List<Map> sort (0) | 2013.05.16 |
Java API for RESTful Web Services (0) | 2013.05.15 |