Skip to content

Commit

Permalink
Merge pull request #350 from 2022-bancow-teamProject/feature/kim-jihun
Browse files Browse the repository at this point in the history
[REFACTOR] Sort Cow Chart Response
  • Loading branch information
kho903 authored Feb 13, 2022
2 parents 25e8981 + 5dd1332 commit ad1e556
Showing 1 changed file with 16 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -38,10 +41,22 @@ default public KoreanCowResponseDto toKoreanCowResponse(KoreanCowCategory catego
cowList.forEach(e ->
info.put(e.getInfoYear(), e.getInfo())
);
infoList.add(info);
Map<Integer, Long> sortInfo = sortMapByKey(info);
infoList.add(sortInfo);
return KoreanCowResponseDto.builder()
.category(category)
.info(infoList)
.build();
}

default public HashMap<Integer, Long> sortMapByKey(Map<Integer, Long> map) {
List<Map.Entry<Integer, Long>> entries = new LinkedList<>(map.entrySet());
entries.sort((o1, o2) -> o1.getKey().compareTo(o2.getKey()));

LinkedHashMap<Integer, Long> result = new LinkedHashMap<>();
for (Map.Entry<Integer, Long> entry : entries) {
result.put(entry.getKey(), entry.getValue());
}
return result;
}
}

0 comments on commit ad1e556

Please sign in to comment.