Sorting a vector using insertion
I am trying to sort a vector of vectors with insertion sort which is what my professor wants done. I also need to pass in int column since the program will be functioning similar to windows explorer. When the user clicks on a column (file, size, type, date modified) int column should be set to 0,1,2,3. (This is done in another part of my program of course) I tried several methods I could think of with the last being an attempt at casting as comparable but that didn't work. Also I can't use java's built in sort. It's not a big deal if I am shown how to incorporate int column I think I can figure that once this actually works. I am using netbeans by the way.
Code Java:
public void insertionSortDescending(int column)
{
Comparable current = null;
int index = 0;
Comparable temp;
for(int j=0; j<folderContents.size();j++)
{
Comparable myobject = (Comparable)folderContents.elementAt(j);
current = myobject;
index = j;
for(int i = j+1; i<folderContents.size();i++){
Comparable myobject2 = (Comparable)folderContents.elementAt(i);
if(current.compareTo(myobject2) < 0)
{
current = myobject2;
index = i;
}
}
temp = (Comparable)folderContents.elementAt(j);
folderContents.setElementAt(folderContents.elementAt(index),j);
folderContents.setElementAt(temp,index);
gui.updateListing(folderContents);
}
I appreciate any positive feedback on this that will lead me in the right direction. I have spent quite a bit of time on the program as a whole but I am only just beginning my java learning experience.
Thanks in advance for anyone's time and effort. It is much appreciated!
In addition here is the first sort I had. It just doesn't use comparable (which is needed since size and date modified won't be sorted correctly otherwise).
Code Java:
public void insertionSortAscending(int column)
{
String current = null;
int index = 0;
Object temp = new Object();
for(int j=0; j<folderContents.size();j++)
{
Object myobject = (Object)folderContents.elementAt(j);
current = myobject.toString();
index = j;
for(int i = j+1; i<folderContents.size();i++){
Object myobject2 = (Object)folderContents.elementAt(i);
String name2 = myobject2.toString();
if(current.compareToIgnoreCase(name2) > 0)
{
current = name2;
index = i;
}
}
temp = folderContents.elementAt(j);
folderContents.setElementAt(folderContents.elementAt(index),j);
folderContents.setElementAt(temp,index);
gui.updateListing(folderContents);
}
Re: Sorting a vector using insertion
Sorry for the double post.
I saw this didn't have any views so I wanted to make sure I posted it according to the rules. Any help would be greatly appreciated.
Thanks!
Re: Sorting a vector using insertion
Don't worry about the views. The counter these days doesn't seem to work properly and only seems to increment upon responses to the original post
Quote:
I tried several methods I could think of with the last being an attempt at casting as comparable but that didn't work.
Please define didn't work. Compilation error? Runtime Error? Incorrect sorting?
Re: Sorting a vector using insertion
Oh, alright. My apologies.
The error I receive is the following:
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.util.Vector cannot be cast to java.lang.Comparable
I am guessing I need to somehow pull just the column and not the vector to sort. What I mean by that is..
folderContents (Primary vector)
list(secondary vector under folderContents containing 4 columns of data)
[Name][size][type][date modified]
I think I need to pull just one column to cast to comparable but I am unsure how to keep the other columns linked with the one being sorted.
Re: Sorting a vector using insertion
What does folderContents contain? Other vectors? If so, it may be easier to group what would be each entry of the nested vectors into a new Class, then sort based upon the values of that class (you can accomplish what you need but you must remove the vector at the appropriate index and sort that vector - them do all the same insertions on all the other vectors...ugly).
Re: Sorting a vector using insertion
Yes, folderContents does contain other vectors. I will try out what you said second (Since we aren't supposed to make any other classes I don't believe). It doesn't need to be pretty it just needs to work is all.
Thanks for the suggestions! I am always accepting any other helpful tips if anyone has done something like this.