File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Sorting Vector of Hashtables by a Hashtable value

 
Heath Travis
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13048
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Arvind Subramanian
Ranch Hand
Posts: 84
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13048
6
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic