Answers for "how to sort a map"

3

TreeMap in java

import java.util.Map;
import java.util.TreeMap;
public class TreeMapExample
{
   public static void main(String[] args) 
   {
      TreeMap<Integer, String> tm = new TreeMap<Integer, String>();    
      tm.put(1000, "Apple");    
      tm.put(1002, "Raspberry");    
      tm.put(1001, "Velvet apple");    
      tm.put(1003, "Banana");    
      for(Map.Entry obj : tm.entrySet())
      {    
         System.out.println(obj.getKey() + " " + obj.getValue());    
      }
   }
}
Posted by: Guest on November-20-2020
0

how to sort a map

bool cmp(pair<T1, T2>& a,
         pair<T1, T2>& b)
{
    return a.second < b.second;
}

where T1 and T2
are the data-types 
that can be the 
same or different.
Posted by: Guest on October-07-2021

Browse Popular Code Answers by Language