Java Map Sort sortMapByValue(Map unsortMap, boolean highFirst)

Here you can find the source of sortMapByValue(Map unsortMap, boolean highFirst)

Description

sort Map By Value

License

Open Source License

Declaration

public static Map<String, Integer> sortMapByValue(Map<String, Integer> unsortMap, boolean highFirst) 

Method Source Code

//package com.java2s;
/*//from   w w  w .  j a v a  2  s  . com
 * Utilities.java
 *
 * Copyright (c) 2009 Jay Lawson <jaylawson39 at yahoo.com>. All rights reserved.
 *
 * This file is part of MekHQ.
 *
 * MekHQ is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * MekHQ 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with MekHQ.  If not, see <http://www.gnu.org/licenses/>.
 */

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

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

import java.util.Map;

public class Main {
    public static Map<String, Integer> sortMapByValue(Map<String, Integer> unsortMap, boolean highFirst) {

        // Convert Map to List
        List<Map.Entry<String, Integer>> list = new LinkedList<Map.Entry<String, Integer>>(unsortMap.entrySet());

        // Sort list with comparator, to compare the Map values
        Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                return (o1.getValue()).compareTo(o2.getValue());
            }
        });

        // Convert sorted map back to a Map
        Map<String, Integer> sortedMap = new LinkedHashMap<String, Integer>();
        if (highFirst) {
            ListIterator<Map.Entry<String, Integer>> li = list.listIterator(list.size());
            while (li.hasPrevious()) {
                Map.Entry<String, Integer> entry = li.previous();
                sortedMap.put(entry.getKey(), entry.getValue());
            }
        } else {
            for (Iterator<Map.Entry<String, Integer>> it = list.iterator(); it.hasNext();) {
                Map.Entry<String, Integer> entry = it.next();
                sortedMap.put(entry.getKey(), entry.getValue());
            }
        }

        return sortedMap;
    }
}

Related

  1. sortMapByValue(Map map, Comparator comparator)
  2. sortMapByValue(Map map, Comparator> valueComparator)
  3. sortMapByValue(Map map, Comparator> comparator)
  4. sortMapByValue(Map map, final boolean descending)
  5. sortMapByValue(Map oriMap)
  6. sortMapByValue(Map oriMap)
  7. sortMapByValue(Map oriMap, final boolean isDesc)
  8. sortMapByValues(Map map, final boolean inverted)
  9. sortMapByValuesDecreasing( final Map mapToSort)