Sorting Vector of Hashtable

Discussions

General J2EE: Sorting Vector of Hashtable

  1. Sorting Vector of Hashtable (1 messages)

    I had met with the scenario to sort this collection. Its Vector> . i provided the example data. [ {endtime=585, starttime=420}, {endtime=675, starttime=660}, {endtime=750, starttime=690}, {endtime=840, starttime=795}, {endtime=960, starttime=900} ] the sort key is "starttime" for every elt in the vector. the final result should be sorted based on the starttime value. please help me its urgent
  2. Re: Sorting Vector of Hashtable[ Go to top ]

    If you are sure that u will have only one set of endtime and starttime in the hashtable; may be you can try something like this: public void testSort() { Vector> vector = new Vector>(); Hashtable table = new Hashtable(); table.put("etime", 900); table.put("stime", 895); vector.add(table); table = new Hashtable(); table.put("etime", 210); table.put("stime", 110); vector.add(table); table = new Hashtable(); table.put("etime", 590); table.put("stime", 210); vector.add(table); System.out.println("B4------------------------------>"); System.out.println(vector); List> l = new ArrayList>(vector); Collections.sort(l, new Comparator>() { public int compare(Hashtable o1, Hashtable o2) { return o1.get("stime").compareTo(o2.get("stime")); } }); System.out.println("After ---------------------------------->"); vector = new Vector>(l); System.out.println(vector); }