Java Map Sort sortMapByValue(Map oriMap, final boolean isDesc)

Here you can find the source of sortMapByValue(Map oriMap, final boolean isDesc)

Description

sort Map By Value

License

Open Source License

Declaration

public static <T> Map<T, Double> sortMapByValue(Map<T, Double> oriMap, final boolean isDesc) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import java.util.Map.Entry;

public class Main {
    public static <T> Map<T, Double> sortMapByValue(Map<T, Double> oriMap) {
        Map<T, Double> sortedMap = new LinkedHashMap<T, Double>();
        if (oriMap != null && !oriMap.isEmpty()) {
            List<Map.Entry<T, Double>> entryList = new ArrayList<Map.Entry<T, Double>>(oriMap.entrySet());
            Collections.sort(entryList, new Comparator<Map.Entry<T, Double>>() {
                public int compare(Entry<T, Double> entry1, Entry<T, Double> entry2) {
                    Double value1 = entry1.getValue();
                    Double value2 = entry2.getValue();

                    return value2.compareTo(value1);
                }//  w  w  w  .j  a v  a 2s .  com
            });
            Iterator<Map.Entry<T, Double>> iter = entryList.iterator();
            Map.Entry<T, Double> tmpEntry = null;
            while (iter.hasNext()) {
                tmpEntry = iter.next();
                sortedMap.put(tmpEntry.getKey(), Double.parseDouble(tmpEntry.getValue() + ""));
            }
        }
        return sortedMap;
    }

    public static <T> Map<T, Double> sortMapByValue(Map<T, Double> oriMap, final boolean isDesc) {
        Map<T, Double> sortedMap = new LinkedHashMap<T, Double>();
        if (oriMap != null && !oriMap.isEmpty()) {
            List<Map.Entry<T, Double>> entryList = new ArrayList<Map.Entry<T, Double>>(oriMap.entrySet());
            Collections.sort(entryList, new Comparator<Map.Entry<T, Double>>() {
                public int compare(Entry<T, Double> entry1, Entry<T, Double> entry2) {
                    Double value1 = entry1.getValue();
                    Double value2 = entry2.getValue();
                    if (isDesc)
                        return value2.compareTo(value1);
                    else
                        return value1.compareTo(value2);
                }
            });
            Iterator<Map.Entry<T, Double>> iter = entryList.iterator();
            Map.Entry<T, Double> tmpEntry = null;
            while (iter.hasNext()) {
                tmpEntry = iter.next();
                sortedMap.put(tmpEntry.getKey(), tmpEntry.getValue());
            }
        }
        return sortedMap;
    }
}

Related

  1. sortMapByValue(Map map, Comparator> comparator)
  2. sortMapByValue(Map map, final boolean descending)
  3. sortMapByValue(Map oriMap)
  4. sortMapByValue(Map unsortMap, boolean highFirst)
  5. sortMapByValue(Map oriMap)
  6. sortMapByValues(Map map, final boolean inverted)
  7. sortMapByValuesDecreasing( final Map mapToSort)
  8. sortMapByValuesDescending( Map aMap)
  9. sortMapDesc(Map map)