value ascending not descendin

import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
public class Solution {
	public static void main(String[] args) {
		HashMap<String, Integer> map = new HashMap<String, Integer>();
		map.put("a", 10);
		map.put("b", 30);
		map.put("c", 50);
		map.put("d", 40);
		map.put("e", 20);
		Map sortedMap = sortByValue(map);
	public static Map sortByValue(Map unsortedMap) {
		Map sortedMap = new TreeMap(new ValueComparator(unsortedMap));
		return sortedMap;
class ValueComparator implements Comparator {
	Map map;
	public ValueComparator(Map map) { = map;
	public int compare(Object keyA, Object keyB) {
		Comparable valueA = (Comparable) map.get(keyA);
		Comparable valueB = (Comparable) map.get(keyB);
		return valueB.compareTo(valueA);

Open in new window

i was trying above example got below output
{a=10, b=30, c=50, d=40, e=20}
{c=50, d=40, b=30, e=20, a=10}

hot to get values in ascending order not descending order as above. please advise
Who is Participating?
return valueA.compareTo(valueB);

Open in new window

is what you need
gudii9Author Commented:
worked. by the way i used example from
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.