반응형
Comparator를 사용할 때, 단순히 reversed를 사용하면 역순으로 정렬이 가능하다 생각하였다.
따라서 단순하게 sorted().reversed().thencomparing().reversed() 형식으로 사용하였는데, 정렬이 되지 않았다.
여러번 정렬을 해야 할 때는 Comparator.comparing().reversed()를 다음과 같이 반복적으로 사용해야 한다.
// 정렬 1
Function<CustomJdbcMap, Long> count = m -> (long) m.get("count");
// 정렬 2
Function<CustomJdbcMap, Integer> pointSort = m -> {
Integer point = 0;
if(Objects.nonNull(m.get("point"))) point = Integer.parseInt(String.valueOf(m.get("point")));
return point;
};
// 정렬 3
Function<CustomJdbcMap, Date> createdDateSort = m -> (Date)m.get("createdDate");
// 여러번 역순 정렬시 사례
resultList = resultList.stream().sorted(Comparator.comparing(count).reversed()
.thenComparing(Comparator.comparing(pointSort).reversed())
.thenComparing(Comparator.comparing(createdDateSort).reversed())).collect(
Collectors.toList());
for(CustomJdbcMap c : resultList){
log.info("================================");
log.info( c.get("count").toString());
log.info( c.get("point").toString());
log.info( c.get("createdDate").toString());
}
정렬이 정렬1 역순, 정렬2 역순, 정렬3 역순 으로 제대로 된 걸 확인할 수 있었다.
15:09:59.847 [http-nio-9030-exec-1] DEBUG c.b.e.c.d.r.p.m.s.P. - <== Total: 4
15:09:59.848 [http-nio-9030-exec-1] INFO c.b.e.a.f.p.s.c. - ================================
15:09:59.848 [http-nio-9030-exec-1] INFO c.b.e.a.f.p.s.c. - 5
15:09:59.848 [http-nio-9030-exec-1] INFO c.b.e.a.f.p.s.c. - 39000
15:09:59.848 [http-nio-9030-exec-1] INFO c.b.e.a.f.p.s.c. - 2022-02-17 08:58:55.0
15:09:59.848 [http-nio-9030-exec-1] INFO c.b.e.a.f.p.s.c. - ================================
15:09:59.848 [http-nio-9030-exec-1] INFO c.b.e.a.f.p.s.c. - 5
15:09:59.848 [http-nio-9030-exec-1] INFO c.b.e.a.f.p.s.c. - 0
15:09:59.848 [http-nio-9030-exec-1] INFO c.b.e.a.f.p.s.c. - 2022-01-17 17:46:50.0
15:09:59.848 [http-nio-9030-exec-1] INFO c.b.e.a.f.p.s.c. - ================================
15:09:59.848 [http-nio-9030-exec-1] INFO c.b.e.a.f.p.s.c. - 4
15:09:59.848 [http-nio-9030-exec-1] INFO c.b.e.a.f.p.s.c. - 800
15:09:59.849 [http-nio-9030-exec-1] INFO c.b.e.a.f.p.s.c. - 2022-01-17 14:40:19.0
15:09:59.849 [http-nio-9030-exec-1] INFO c.b.e.a.f.p.s.c. - ================================
15:09:59.849 [http-nio-9030-exec-1] INFO c.b.e.a.f.p.s.c. - 4
15:09:59.849 [http-nio-9030-exec-1] INFO c.b.e.a.f.p.s.c. - 0
15:09:59.849 [http-nio-9030-exec-1] INFO c.b.e.a.f.p.s.c. - 2022-01-17 14:40:19.0
반응형
'업무 > 실수 노트' 카테고리의 다른 글
[JAVA] setTimeZone (0) | 2022.02.25 |
---|---|
[SQL] DATEDIFF, TIMEDIFF (0) | 2022.02.22 |
[JAVA] 조건절 (0) | 2022.02.16 |
[SQL] GROUP BY 2개 이상 (0) | 2022.02.15 |
[Vue.js] flatpickr (0) | 2022.02.11 |