I was trying to sort my ArrayList.
java.util.ArrayList arList= new java.util.ArrayList();
arList=getList();
java.util.Collections.sort(arList);
where my getList() function here
public ArrayList getList() throws Exception
{
ArrayList listItems = new ArrayList();
//Query executing here..............!
while (rs.next())
{
HashMap hashList = new HashMap();
hashList.put("name",rs.getString(1));
hashList.put("id",rs.getBigDecimal(2));
listItems.add(hashList);
}
return listItems;
}
But I am facing error :java.lang.ClassCastException: java.util.HashMap cannot be cast to java.lang.Comparable
ArrayList
? Your list contains a number ofHashMaps
. Are you trying to sort it based on the key of the map or the value of the map? – radimpe Sep 26 '12 at 5:38HashMap
s here? You should be creating a proper object class to hold names and IDs. – Louis Wasserman Sep 26 '12 at 17:24