Java Map Sort sortByValue(final Map map, final boolean asc)

Here you can find the source of sortByValue(final Map map, final boolean asc)

Description

sort a Map on the values.

License

Open Source License

Parameter

Parameter Description
map a parameter
asc a parameter

Return

the map, sorted by the value in ascending or descending order (according to asc)

Declaration

public static <K, V extends Comparable<? super V>> Map<K, V> sortByValue(final Map<K, V> map,
        final boolean asc) 

Method Source Code

//package com.java2s;
/**// w w w. j ava2 s .co  m
 *  MapUtil
 *  Copyright 02.10.2016 by Michael Peter Christen, @0rb1t3r
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *  
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *  
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with this program in the file lgpl21.txt
 *  If not, see <http://www.gnu.org/licenses/>.
 */

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

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

public class Main {
    /**
     * sort a Map<Key, Value> on the values. This algorithm is taken from
     * http://stackoverflow.com/questions/109383/sort-a-mapkey-value-by-values-java
     * provided by tunaki http://stackoverflow.com/users/1743880/tunaki
     * It was extended with a 'asc' attribute to select ascending (asc = true)
     * or descending (desc) order.
     * @param map
     * @param asc
     * @return the map, sorted by the value in ascending or descending order (according to asc)
     */
    public static <K, V extends Comparable<? super V>> Map<K, V> sortByValue(final Map<K, V> map,
            final boolean asc) {

        // put the given map into a list with Map.Entry objects
        List<Map.Entry<K, V>> list = new LinkedList<Map.Entry<K, V>>(map.entrySet());
        // declare a sort method for such kind of lists and sort it.
        Collections.sort(list, new Comparator<Map.Entry<K, V>>() {
            @Override
            public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2) {
                return asc ? (o1.getValue()).compareTo(o2.getValue()) : (o2.getValue()).compareTo(o1.getValue());
            }
        });

        // create a result map and put the ordered list inside.
        // The map is a linked map which has a defined object order
        Map<K, V> result = new LinkedHashMap<K, V>();
        for (Map.Entry<K, V> entry : list) {
            result.put(entry.getKey(), entry.getValue());
        }
        return result;
    }
}

Related

  1. sortByValue( Map map)
  2. sortByValue( Map map, final boolean ascendingValues)
  3. sortByValue(final Map m)
  4. sortByValue(final Map map)
  5. sortByValue(final Map map)
  6. sortByValue(Map map)
  7. sortByValue(Map map)
  8. sortByValue(Map map, Comparator aComp)
  9. sortByValue(Map map)