aspose file tools*
The moose likes Java in General and the fly likes Sorting Vector of Hashtables by a Hashtable value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Sorting Vector of Hashtables by a Hashtable value" Watch "Sorting Vector of Hashtables by a Hashtable value" New topic
Author

Sorting Vector of Hashtables by a Hashtable value

Heath Travis
Greenhorn

Joined: Nov 12, 2003
Posts: 1
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"));
}

}
}
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12671
    
    5
Seems to me you need to look at the sort method of the Collections class - the one that takes a List and a Comparator and sorts the list. Vector implements the List interface so you can use it directly.
What you have to do is create a Comparator that can look into the Hashtables for a particular named value and do the comparison.
Collections and Comparator are in the java.util package.
Bill


Java Resources at www.wbrogden.com
Arvind Subramanian
Ranch Hand

Joined: Jul 25, 2008
Posts: 84
William Brogden wrote:Seems to me you need to look at the sort method of the Collections class - the one that takes a List and a Comparator and sorts the list. Vector implements the List interface so you can use it directly.
What you have to do is create a Comparator that can look into the Hashtables for a particular named value and do the comparison.
Collections and Comparator are in the java.util package.
Bill



can you please explain it, i am new to java, but need this urgently

the data i need to sort is
[{endtime=585, starttime=420},
{endtime=675, starttime=660},
{endtime=750, starttime=690},
{endtime=840, starttime=795},
{endtime=960, starttime=900}]

based on the starttime

"Many of lifes failure are people who did not realize how close they were to success when they gave up."
-Thomas Edison
~ar~stutzen~
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12671
    
    5
1. Please Dont wake the zombies
2. A google search for Comparator will find you plenty of examples such as this one.

Bill
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sorting Vector of Hashtables by a Hashtable value
 
Similar Threads
getting the int value
problem about keys of hashtable
Threads access to object and performance/object in ServletContext: value or reference
hashtable with Vector
Sorting of hashtables in a vector