Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Sorting a vector using insertion

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

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


  2. #2
    Junior Member
    Join Date
    Oct 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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!

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default 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

    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?

  4. #4
    Junior Member
    Join Date
    Oct 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  5. #5
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default 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).

  6. #6
    Junior Member
    Join Date
    Oct 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

Similar Threads

  1. Unit Vector
    By mingming8888 in forum Java Theory & Questions
    Replies: 2
    Last Post: October 14th, 2010, 02:53 PM
  2. Pseudo code of insertion sort linked list
    By sungirl in forum Algorithms & Recursion
    Replies: 1
    Last Post: May 23rd, 2010, 09:25 AM
  3. [SOLVED] 2D Vector
    By nasi in forum Collections and Generics
    Replies: 2
    Last Post: May 6th, 2010, 01:42 AM
  4. vector
    By sriraj.kundan in forum Java Theory & Questions
    Replies: 8
    Last Post: August 12th, 2009, 10:17 AM
  5. Replies: 1
    Last Post: May 19th, 2009, 09:19 AM

Tags for this Thread