• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Sorting Vector of Hashtables by a Hashtable value

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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"));
}

}
}
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
If you are using a wood chipper, you are doing it wrong. Even on this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic