content: /* 
   * Copyright (C) 2015 Adrien Guille <adrien.guille@univ-lyon2.fr>
   *
   * This program 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.
   *
   * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
   */
  package main.java.fr.ericlab.sondy.core.utils;
  
  import java.util.Collections;
  import java.util.Comparator;
  import java.util.LinkedHashMap;
  import java.util.LinkedList;
  import java.util.List;
  import java.util.Map;
  
  /**
   *
   *   @author Adrien GUILLE, Laboratoire ERIC, Université Lumière Lyon 2
   */
  public class HashMapUtils {
      public static <K, V extends Comparable<? super V>> Map<K, V> sortByAscValue( Map<K, V> map ){
          List<Map.Entry<K, V>> list =
              new LinkedList<>( map.entrySet() );
          Collections.sort( list, new Comparator<Map.Entry<K, V>>()
          {
              @Override
              public int compare( Map.Entry<K, V> o1, Map.Entry<K, V> o2 )
              {
                  return (o1.getValue()).compareTo( o2.getValue() );
              }
          } );
  
          Map<K, V> result = new LinkedHashMap<>();
          for (Map.Entry<K, V> entry : list)
          {
              result.put( entry.getKey(), entry.getValue() );
          }
          return result;
      }
      
      public static <K, V extends Comparable<? super V>> Map<K, V> sortByDescValue( Map<K, V> map ){
          List<Map.Entry<K, V>> list =
              new LinkedList<>( map.entrySet() );
          Collections.sort( list, new Comparator<Map.Entry<K, V>>()
          {
              @Override
              public int compare( Map.Entry<K, V> o1, Map.Entry<K, V> o2 )
              {
                  return -(o1.getValue()).compareTo( o2.getValue() );
              }
          } );
  
          Map<K, V> result = new LinkedHashMap<>();
          for (Map.Entry<K, V> entry : list)
          {
              result.put( entry.getKey(), entry.getValue() );
          }
          return result;
      }
  }
  