Java HashMap Sort sortHashMapByValues( HashMap> passedMap, String key0)

Here you can find the source of sortHashMapByValues( HashMap> passedMap, String key0)

Description

Sort hash map by values key0.

License

Open Source License

Parameter

Parameter Description
passedMap a parameter
key0 COUNT or SUM

Return

LinkedHashMap

Declaration

static public LinkedHashMap<String, HashMap<String, Object>> sortHashMapByValues(
        HashMap<String, HashMap<String, Object>> passedMap, String key0) 

Method Source Code

//package com.java2s;
/*//from ww w.j a v a 2  s .c  o m
 *-------------------
 * The Utils.java is part of ASH Viewer
 *-------------------
 * 
 * ASH Viewer 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.
 * 
 * ASH Viewer 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 ASH Viewer.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Copyright (c) 2009, Alex Kardapolov, All rights reserved.
 *
 */

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;

import java.util.Map.Entry;

public class Main {
    /**
     * Sort hash map by values key0.
     * 
     * @param passedMap
     * @param key0 COUNT or SUM
     * @return LinkedHashMap
     */
    static public LinkedHashMap<String, HashMap<String, Object>> sortHashMapByValues(
            HashMap<String, HashMap<String, Object>> passedMap, String key0) {

        List mapKeys = new ArrayList();
        List mapValues = new ArrayList();

        for (Entry<String, HashMap<String, Object>> me : passedMap.entrySet()) {
            mapKeys.add(me.getKey());
            mapValues.add(me.getValue().get(key0));
        }

        Collections.sort(mapValues);
        Collections.sort(mapKeys);
        Collections.reverse(mapValues);

        LinkedHashMap<String, HashMap<String, Object>> sortedMap = new LinkedHashMap<String, HashMap<String, Object>>();

        Iterator valueIt = mapValues.iterator();
        while (valueIt.hasNext()) {
            Object val = valueIt.next();
            Iterator keyIt = mapKeys.iterator();

            while (keyIt.hasNext()) {
                Object key = keyIt.next();
                String comp1 = passedMap.get(key).get(key0).toString();
                String comp2 = val.toString();

                if (comp1.equals(comp2)) {
                    sortedMap.put((String) key, passedMap.get(key));
                    passedMap.remove(key);
                    mapKeys.remove(key);
                    break;
                }
            }
        }
        return sortedMap;
    }
}

Related

  1. getSortedLinkedHashMap(Map bitCounts, Comparator comparator)
  2. sortByComparator(HashMap unsortMap, final boolean ascendingorder)
  3. sortByValue(HashMap map)
  4. sortedScoreMap(HashMap unSortedMap)
  5. sortHashMapByKeys(HashMap passedMap, boolean ascending)
  6. sortHashMapByValues(HashMap passedMap, boolean ascending)
  7. sortHashMapByValues(HashMap passedMap)
  8. sortMapByValue(HashMap map, final int sort)

  9. HOME | Copyright © www.java2s.com 2016