2 Replies Latest reply: Sep 4, 2007 3:50 PM by 807605 RSS

    Sorting a vector of string arrays

    807605
      Thanks for the previous help on vectors. Now, I need to sort the vector. This code gets an error: java.lang.ClassCastException.
      Any ideas how I can sort this vector?
      import java.util.*;
      public class VecTest2 
      {
         public static void main(String[] args) 
         {
              Vector vector = new Vector();
              String[] items = null;
              String orTab = null;
              String orCr8 = null;
              String orRow = null;
              String firstthing = null;
              String secondthing = null;
              String thirdthing = null;
              
              orTab = "Table3"; orCr8 = "Create Table3"; orRow = "Add col to Table3";
              vector.add(new String[]{orTab, orCr8, orRow});
              orTab = "Table1"; orCr8 = "Create Table1"; orRow = "Add col to Table1";
              vector.add(new String[]{orTab, orCr8, orRow});
              orTab = "Table2"; orCr8 = "Create Table2"; orRow = "Add col to Table2";
              vector.add(new String[]{orTab, orCr8, orRow});
      
      
              for (Enumeration e = vector.elements() ; e.hasMoreElements() ;) 
              {
                    String attributeArray[] = (String[])e.nextElement();
                    firstthing = attributeArray[0];
                    secondthing = attributeArray[1];
                    thirdthing = attributeArray[2];
                    System.out.println(firstthing);
                    System.out.println(secondthing);
                    System.out.println(thirdthing);
              }
              
              Collections.sort(vector);
              
              System.out.println("After sort.....");
              for (Enumeration e = vector.elements() ; e.hasMoreElements() ;) 
              {
                    String attributeArray[] = (String[])e.nextElement();
                    firstthing = attributeArray[0];
                    secondthing = attributeArray[1];
                    thirdthing = attributeArray[2];
                    System.out.println(firstthing);
                    System.out.println(secondthing);
                    System.out.println(thirdthing);
              }
              
          }
      }