Advertisement
rachmadi

Sorting

Jan 8th, 2016
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.42 KB | None | 0 0
  1. package com.rais;
  2.  
  3. import java.util.Collections;
  4. import java.util.Comparator;
  5. import java.util.HashMap;
  6. import java.util.LinkedHashMap;
  7. import java.util.LinkedList;
  8. import java.util.List;
  9. import java.util.Map;
  10. import java.util.Map.Entry;
  11.  
  12. public class SortMapByValue
  13. {
  14.     public static boolean ASC = true;
  15.     public static boolean DESC = false;
  16.  
  17.     public static void main(String[] args)
  18.     {
  19.  
  20.         // Creating dummy unsorted map
  21.         Map<String, Integer> unsortMap = new HashMap<String, Integer>();
  22.         unsortMap.put("B", 55);
  23.         unsortMap.put("A", 80);
  24.         unsortMap.put("D", 20);
  25.         unsortMap.put("C", 70);
  26.  
  27.         System.out.println("Before sorting......");
  28.         printMap(unsortMap);
  29.  
  30.         System.out.println("After sorting ascending order......");
  31.         Map<String, Integer> sortedMapAsc = sortByComparator(unsortMap, ASC);
  32.         printMap(sortedMapAsc);
  33.  
  34.  
  35.         System.out.println("After sorting descindeng order......");
  36.         Map<String, Integer> sortedMapDesc = sortByComparator(unsortMap, DESC);
  37.         printMap(sortedMapDesc);
  38.  
  39.     }
  40.  
  41.     private static Map<String, Integer> sortByComparator(Map<String, Integer> unsortMap, final boolean order)
  42.     {
  43.  
  44.         List<Entry<String, Integer>> list = new LinkedList<Entry<String, Integer>>(unsortMap.entrySet());
  45.  
  46.         // Sorting the list based on values
  47.         Collections.sort(list, new Comparator<Entry<String, Integer>>()
  48.         {
  49.             public int compare(Entry<String, Integer> o1,
  50.                     Entry<String, Integer> o2)
  51.             {
  52.                 if (order)
  53.                 {
  54.                     return o1.getValue().compareTo(o2.getValue());
  55.                 }
  56.                 else
  57.                 {
  58.                     return o2.getValue().compareTo(o1.getValue());
  59.  
  60.                 }
  61.             }
  62.         });
  63.  
  64.         // Maintaining insertion order with the help of LinkedList
  65.         Map<String, Integer> sortedMap = new LinkedHashMap<String, Integer>();
  66.         for (Entry<String, Integer> entry : list)
  67.         {
  68.             sortedMap.put(entry.getKey(), entry.getValue());
  69.         }
  70.  
  71.         return sortedMap;
  72.     }
  73.  
  74.     public static void printMap(Map<String, Integer> map)
  75.     {
  76.         for (Entry<String, Integer> entry : map.entrySet())
  77.         {
  78.             System.out.println("Key : " + entry.getKey() + " Value : "+ entry.getValue());
  79.         }
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement