public static Map sortMap(Map unsortedMap) {
if (unsortedMap != null && unsortedMap.size() > 1) {
List list = new LinkedList(unsortedMap.entrySet());
Collections.sort(list, new Comparator() {
public int compare(Object o1, Object o2) {
return ((Comparable) ((Map.Entry) (o1)).getValue()).compareTo(((Map.Entry) (o2)).getValue());
}
});
Map sortedMap = new LinkedHashMap();
for (Iterator it = list.iterator(); it.hasNext();) {
Map.Entry entry = (Map.Entry) it.next();
sortedMap.put(entry.getKey(), entry.getValue());
}
return sortedMap;
} else {
return unsortedMap;
}
}
Monday, September 23, 2013
Compose sorted map using an unsorted map's values in Java
You might have unsorted map and sometimes you might need to sort them in some requirements using it's values, not the keys. You can have a util method like this to do it. Make sure the value's class must implement the Comparable interface to mention the sort criteria.
Disabling all the click events under a div element using jQuery
You might have a requirement to disable all the click events under a particular div. Following is a way to disable click events.
var disable = function() {
var divElement = $("#myDiv");
var disableMask = $("<div></div>");
disableMask.attr("id", divElement.prop("id") + "_mask");
disableMask.css({
"position" : "absolute",
"width" : "100%",
"height" : "100%",
"zIndex" : "100"
});
divElement.append(disableMask);
};
var enable = function() {
var divElement = $("#myDiv");
var disableMask = $("#" + divElement.prop("id") + "_mask");
if(disableMask != null){
$("#" + divElement.prop("id") + "_mask").remove();
}
};
Location:
Colombo, Sri Lanka
Absolute and Relative HTML div positioning using jQuery
Lets say you have multiple HTML div elements to be absolute positioned under another div element, then you need to make parent div positioned as relative and then the child div elements to be positioned as absolute. You can change the other css attributes as well using jQuery on child div elements.
Now the child div element is placed under absolutely within the parent div according to the css properties of top, left, right, bottom. You can have any number of child div elements positioned like this and change the positioning properties accordingly.
var parentDiv = $("#parent");
parentDiv.css({
"position" : "relative",
"height" : "100px",
"width" : "200px"
});
var childDiv = $("<div></div>").appendTo(parentDiv);
childDiv.prop("id","child");
childDiv.css({
"position" : "absolute",
"height" : "50px",
"width" : "50px",
"top" : "10px",
"left" : "10px"
});
Now the child div element is placed under absolutely within the parent div according to the css properties of top, left, right, bottom. You can have any number of child div elements positioned like this and change the positioning properties accordingly.
Subscribe to:
Comments (Atom)