Wrox Home  
Search P2P Archive for: Go

  Return to Index  

pro_java thread: Sorting in a Vector


Message #1 by tech@a... on Sun, 24 Jun 2001 20:41:39
Hi All-

I have the following piece of code in a program I am working on. I am 
trying to sort the Vector according to a Date object in decending order 
(earliest to latest). The following piece of code sorts it in acending 
order. If I change the < to a > in the code segemnt "if (rt.getSubmit
().compareTo(tmpObject.getSubmit()) < 0)"I get an array out of bounds 
exception. 

Here is the code:
private Vector parseFileArray(File[] ordArray) throws NumberFormatException
{
Vector tmpVec = new Vector(ordArray.length);
        for (int i = 0; i < ordArray.length; i++){
            try{
                //create the object input stream for ordArray[i]
                FileInputStream input = new FileInputStream(ordArray[i]);
                ObjectInputStream obIn = new ObjectInputStream(input);
                ReservationUtility tmpObject = (ReservationUtility)
obIn.readObject();
                if (i == 0) tmpVec.add(i, tmpObject);
                for (int k = 0; k < i; k++){
                    ReservationUtility rt = (ReservationUtility)tmpVec.get
(k);
                    if (rt.getSubmit().compareTo(tmpObject.getSubmit()) < 
0){
                        tmpVec.add(k,tmpObject);
                        k = i + 1;//end for loop
                    }else{
                        if (k == i - 1 && k != 0) tmpVec.add(k+1, 
tmpObject);
                        k = i + 1;//end for loop  
                    }
                }
            }catch (IOException e){
                System.out.println(e);
            }catch (ClassNotFoundException e){
                System.out.println(e);
            }
            
        }
        //this portion of code I added to reverse the order of the vector
        //it accomplishes the goal, however it slows the process down
        //quite a bit.
        Vector retVec = new Vector(tmpVec.size());
        int count = tmpVec.size();
        for (int i = 0; i < count; i++){
            retVec.add(i, tmpVec.get(count - 1 - i));
        }
        return retVec;
    }

Any help, would be much appreciated.
Kind Regards,
Kyle

  Return to Index