Java HashMap Sort sortMapByValue(HashMap map, final int sort)

Here you can find the source of sortMapByValue(HashMap map, final int sort)

Description

sort Map By Value

License

Apache License

Declaration

public static <K, V> List<Map.Entry<K, V>> sortMapByValue(HashMap<K, V> map, final int sort) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main {

    public static <K, V> List<Map.Entry<K, V>> sortMapByValue(HashMap<K, V> map, final int sort) {
        List<Map.Entry<K, V>> orderList = new ArrayList<Map.Entry<K, V>>(map.entrySet());
        Collections.sort(orderList, new Comparator<Map.Entry<K, V>>() {
            @Override/*from  w ww  . ja v a2  s  . c  o m*/
            @SuppressWarnings("unchecked")
            public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2) {
                return (((Comparable<V>) o2.getValue()).compareTo(o1.getValue())) * sort;
            }
        });
        return orderList;
    }
}

Related

  1. sortedScoreMap(HashMap unSortedMap)
  2. sortHashMapByKeys(HashMap passedMap, boolean ascending)
  3. sortHashMapByValues( HashMap> passedMap, String key0)
  4. sortHashMapByValues(HashMap passedMap, boolean ascending)
  5. sortHashMapByValues(HashMap passedMap)