From 5dd1332f75393cbdf65b88f87bb8296e0b2ff789 Mon Sep 17 00:00:00 2001 From: kho903 Date: Mon, 14 Feb 2022 00:30:53 +0900 Subject: [PATCH] feat: add sortMapByKey for sorting chart year [#349] --- .../domain/sub/chart/mapper/ChartMapper.java | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/spring/src/main/java/com/bancow/bancowback/domain/sub/chart/mapper/ChartMapper.java b/spring/src/main/java/com/bancow/bancowback/domain/sub/chart/mapper/ChartMapper.java index c0d88921..4b9e4f38 100644 --- a/spring/src/main/java/com/bancow/bancowback/domain/sub/chart/mapper/ChartMapper.java +++ b/spring/src/main/java/com/bancow/bancowback/domain/sub/chart/mapper/ChartMapper.java @@ -1,7 +1,10 @@ package com.bancow.bancowback.domain.sub.chart.mapper; import java.util.ArrayList; +import java.util.Collections; import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.LinkedList; import java.util.List; import java.util.Map; @@ -38,10 +41,22 @@ default public KoreanCowResponseDto toKoreanCowResponse(KoreanCowCategory catego cowList.forEach(e -> info.put(e.getInfoYear(), e.getInfo()) ); - infoList.add(info); + Map sortInfo = sortMapByKey(info); + infoList.add(sortInfo); return KoreanCowResponseDto.builder() .category(category) .info(infoList) .build(); } + + default public HashMap sortMapByKey(Map map) { + List> entries = new LinkedList<>(map.entrySet()); + entries.sort((o1, o2) -> o1.getKey().compareTo(o2.getKey())); + + LinkedHashMap result = new LinkedHashMap<>(); + for (Map.Entry entry : entries) { + result.put(entry.getKey(), entry.getValue()); + } + return result; + } }