Wrox Home  
Search P2P Archive for: Go

  Return to Index  

pro_java thread: RE: Sorting in a Vector


Message #1 by Chanoch Wiggers <ChanochW@w...> on Thu, 16 Aug 2001 12:19:43 +0100
can I ask why you are doing it this way? it is probably better to get an
array from the vector and since Date implements comparable anyway, you can
call Array.sort() on the array, then using your array to access the sorted
data. since you can query an array of its length, you can iterate through it
forward or backward with ease. 

alternatively, Vecto implements List and so can be sorted using the
following line:

Collections.sort((List)myVector)

where myVector is your vector. 

You can now either reverse the order if you like using:

Collections.reverse((List)myVector);

or get an array from your vector and then be able to access the array in
either direction as above.

chanoch



-----Original Message-----
From: tech@a... [mailto:tech@a...]
Sent: 24 June 2001 21:42
To: Professional Java
Subject: [pro_java] Sorting in a Vector


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