@SuppressWarnings("unchecked")
	public static HashMap<?,?> sortByValue(HashMap<?,?> map, final int flag) {
		// flag = 0 decreasing order otherwise increasing
		List list = new LinkedList(map.entrySet());
		Collections.sort(list, new Comparator() {
			public int compare(Object o1, Object o2) {
				if(flag == 0 )
					return ((Comparable) ((Map.Entry) (o2)).getValue())
						.compareTo(((Map.Entry) (o1)).getValue());
				else
					return ((Comparable) ((Map.Entry) (o1)).getValue())
					.compareTo(((Map.Entry) (o2)).getValue());
			}
		});

		HashMap result = new LinkedHashMap();
		for (Iterator it = list.iterator(); it.hasNext();) {
			Map.Entry entry = (Map.Entry) it.next();
			result.put(entry.getKey(), entry.getValue());
		}
		return result;
	}