Tag Archives: Sort

[typescript] object 내에 있는 key를 기준으로 object 내부를 정렬하기 (sort, order by…)

By | 2월 14, 2023

소스 /** * 인자로 받은 object를 key 기준으로 내부 정렬하여 리턴한다. * * – 한계점 * – 1depth 만 되는 것처럼 보인다. */ export const sortByKey = (unordered: any = {}) => { return Object.keys(unordered) .sort() .reduce((obj: any = {}, key: any) => { obj[key] = unordered[key]; return obj; }, {}); }; 참고 https://stackoverflow.com/questions/5467129/sort-javascript-object-by-key

Java Map 내의 요소 정렬(sort)하는 코드 샘플

By | 9월 16, 2019

private Map<String, OwnerVO> getSortedMapByOwnerTotChgVol(Map<String, OwnerVO> ownerMap) { List<Map.Entry<String, OwnerVO>> sortList = new LinkedList<>(ownerMap.entrySet()); Collections.sort(sortList, new Comparator<Map.Entry<String, OwnerVO>>() { @Override public int compare(Entry<String, OwnerVO> o1, Entry<String, OwnerVO> o2) { return ((o1.getValue().getTotChgVol() – o2.getValue().getTotChgVol()) < 0 ? 1 : -1); } }); Map<String, OwnerVO> sortedMap = new LinkedHashMap<>(); Iterator<Map.Entry<String, OwnerVO>> iter = sortList.iterator(); while(iter.hasNext()) { Map.Entry<String, OwnerVO>… Read More »