Supose i have name Mink,Mark,Aashis. How do compare them and arrange them according to alphabetical order. Collections.sort() dont work for me. Here i should have results Aashis,Mark and Mark.
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
HashMap<String, String> map = new HashMap<String, String>();
map.put("name", XMLfunctions.getValue(e, "name"));
mylist.add(map);
Collections.sort( mylist, new Comparator<HashMap<String,String>>(){
public int compare(HashMap<String, String> lhs,
HashMap<String, String> rhs) {
//how do i compare string name
return 0;
}
});
Here “name” is the Key that you have provided at the time of insertion in the HashMap using put method.
EXTRA
If you want to parse double values then you can use the Double.parse() method.
return Double.compare(o1.get(value1), o1.get(value2));
NOTE – The nice thing about this approach is that you then sort any object by any attribute or even a combination of attributes. For example if you have objects of type Person with an attribute income and dataOfBirth you could define different implementations of Comparator and sort the objects according to your needs.
Hope this will help you.