Java Map Sort sortByValue( final Map map)

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

Description

Sorts a Map by value Partially buggy due to http://stackoverflow.com/questions/109383/how-to-sort -a-mapkey-value-on-the-values-in-java/1283722#1283722

License

Open Source License

Parameter

Parameter Description
map a parameter

Return

SortedMap by Value

Declaration

@Deprecated
public static <K, V extends Comparable<? super V>> List<Map.Entry<K, V>> sortByValue(
        final Map<K, V> map) 

Method Source Code

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

import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class Main {
    /**/*ww w .  j a  v a2 s .  co  m*/
     * Sorts a Map by value
     * 
     * Partially buggy due to
     * http://stackoverflow.com/questions/109383/how-to-sort
     * -a-mapkey-value-on-the-values-in-java/1283722#1283722
     * 
     * @param map
     * @return SortedMap by Value
     */
    @Deprecated
    public static <K, V extends Comparable<? super V>> List<Map.Entry<K, V>> sortByValue(
            final Map<K, V> map) {
        final List<Map.Entry<K, V>> list = new LinkedList<Map.Entry<K, V>>(
                map.entrySet());
        Collections.sort(list, new Comparator<Map.Entry<K, V>>() {
            @Override
            public int compare(final Map.Entry<K, V> op1,
                    final Map.Entry<K, V> op2) {
                return (op2.getValue()).compareTo(op1.getValue());
            }
        });
        return list;
    }
}

Related

  1. sortByKey(T map, final boolean descending)
  2. sortByKeys( Map map)
  3. sortByKeys(Map map)
  4. sortByMap(List> lists)
  5. sortByMap(Map map, int size)
  6. sortByValue( Map map)
  7. sortByValue( Map map, final boolean ascendingValues)
  8. sortByValue(final Map m)
  9. sortByValue(final Map map)