I inherited an application that receives a Vector filled with Hashtable objects. They enumerate through the vector and print out the values of each Hashtable object. Now they want these values printed out in a sorted order by one of the key values from the Hashtable! To make matters worse, the values are not unique. Can anyone provide me with some good sample code showing how to do this sort? Below I have provided and example of what I am trying to do. Thanks in advance!
-------------
import java.util.Hashtable;
import java.util.Vector;
import java.util.Enumeration;
public class SortHashtable {
public static void main(
String[] args) {
// Create a vector filled with hashtable objects
Vector v = new Vector();
Hashtable ht = new Hashtable();
ht.put("Year", "2003");
ht.put("Name", "Fred");
ht.put("State", "AZ");
v.addElement(ht);
ht = new Hashtable();
ht.put("Year", "2000");
ht.put("Name", "Sue");
ht.put("State", "TX");
v.addElement(ht);
ht = new Hashtable();
ht.put("Year", "2001");
ht.put("Name", "Joe");
ht.put("State", "KS");
v.addElement(ht);
ht = new Hashtable();
ht.put("Year", "2000");
ht.put("Name", "Sally");
ht.put("State", "CA");
v.addElement(ht);
/*
Below, I pull out the values of each hashtable out
of the vector and print them out.
How do I print these out in decending order by year?
What code do i need to do this type of sort?
Please note that there can be duplicate "Year" values.
*/
Enumeration enum = null;
ht= new Hashtable();
enum = v.elements();
while(enum.hasMoreElements()) {
ht = (Hashtable)enum.nextElement();
System.out.println(ht.get("Year") + ", " + ht.get("Name") + ", " + ht.get("State"));
}
}
}