13
Jan

Maps sort based on Key values

	/*
	 *  You cannot sort the map itself based on values. But you can get list of entries and sort it.
	 */
	@Test
	public void myFunction() {
		SortedMap<String, Double> myMap = new TreeMap<String, Double>(new Comparator<String>() {
			public int compare(String o1, String o2) {
				return o1.compareTo(o2);
			}
		});
		myMap.put("Mon", 12.0);
		myMap.put("Tue", 12.1);
		myMap.put("Wed", 12.2);
		myMap.put("Thu", 12.3);
		myMap.put("Fri", 12.4);		
		
		System.out.println(myMap);
		
		
		
		List<Map.Entry<String, Double>> entryList = new ArrayList<Map.Entry<String, Double>>(myMap.entrySet());

	    Collections.sort(entryList, new Comparator<Map.Entry<String, Double>>() {
	        @Override
	        public int compare(Entry<String, Double> o1, Entry<String, Double> o2) {
	            return o1.getValue().compareTo(o2.getValue());
	        }
	    });
	    
	    System.out.println(entryList);
	}