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.
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).
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); }