Solved

I need to compare a vector of vectors using the Comparable interface.

Posted on 2011-09-26
8
299 Views
Last Modified: 2012-05-12
Here is my code so far:

 public static void ascendingVectorSort(Vector objList, int column)
    {
        for (int i = 1; i < objList.size(); i++) {
            Object element = ((Vector)objList.elementAt(i)).elementAt(column);
            Comparable firstUnsorted = (Comparable)element;
            int index = i - 1;
           
            Object element2 = ((Vector)objList.elementAt(index)).elementAt(column);
            while (index >= 0 && (((Comparable)(element2)).compareTo(firstUnsorted)) > 0) {
                objList.setElementAt((objList.elementAt(index)), (index + 1));
                index--;
            }
            objList.setElementAt(((Vector)objList.elementAt(i)), (index + 1));
        }
    }

The goal is using a JTable with a list of a list of items. You use the table to view the directory on your computer. I have everything figured out except for a couple of lines of code. I figured out how to sort a regular vector of objects using comparable but I can't figure out how to sort the vector with a list of vectors in it. I cannot use the Collections.sort() method and I must use the Comparable interface as you see above. This is for a java program.

These two lines I think are the problem:

objList.setElementAt((objList.elementAt(index)), (index + 1));

objList.setElementAt(((Vector)objList.elementAt(i)), (index + 1));

Thanks!
0
Comment
Question by:re8622
  • 3
  • 3
8 Comments
 
LVL 9

Expert Comment

by:oheil
ID: 36708582
This may be a problem:
class Vector does not implement interface Comparable, you must implement it:
http://download.oracle.com/javase/6/docs/api/java/lang/Comparable.html

And you should not change the input list objList during the loop.
Make a copy of the input list and change the copy. After the loop set the original input list to the new ordered copy.

Oli
0
 

Author Comment

by:re8622
ID: 36711044
I've been able to compare the element that I need in the vector of vectors. The problem I'm having is that it doesn't insert the the information correctly. It takes the first one and then inserts it into each vector resulting in a vector of vectors of the same item. This particular line:

objList.setElementAt(((Vector)objList.elementAt(i)), (index + 1));

Is the problem, but I'm not quite sure what to do to fix it. I've compared it to a code that I wrote for just a vector of objects and I don't have this problem. I'm not quite sure how copying the original list into a new list would solve the problem since it doesn't insert properly to begin with.

Thanks!
0
 
LVL 9

Accepted Solution

by:
oheil earned 500 total points
ID: 36715997
Yes you are right:
t takes the first one and then inserts it into each vector resulting in a vector of vectors of the same item.

Thats what your code is doing.
imagine i = 1
than index = 0
The while loop runs exactly one time and puts the 0th element into 1th element.
After this the 1th element (which is now a copy of 0th element) is put into 0th element.

Therefore the first step i=1 sets the 0th and the 1th element equal to the 0th element.

Thats what you do but probably not intentional.

I suggest something like that:

 public static void ascendingVectorSort(Vector objList, int column)
    {
        int n = objList.size();
        do {
                int something_done=0;
                for (int i = 0; i < n -1; i++) {
                        Object element1 = ((Vector)objList.elementAt(i)).elementAt(column);
                        Object element2 = ((Vector)objList.elementAt(i+1)).elementAt(column);
                        // if( element1 > element2 ) 
                        if( (((Comparable)(element2)).compareTo(element1))>0 ) {
                                objList.setElementAt(element2, i);
                                objList.setElementAt(element1, i+1);
                                something_done=1;
                        }
                }
                n--;
        } while(something_done==1 && n > 1);
    }

Open in new window


I didnt tested and there probably are some syntax errors, but its bubblesort.

Oli


0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:re8622
ID: 36718127
Thank you! This is great. I unfortunately need to figure out the sort method using either Selection Sort, Insertion Sort, Merge Sort, or Quick Sort. I'm sorry that I didn't specify this earlier.

I also ran the code and it still doesn't quite sort it. I had to alter it a bit so here it is:

public static void ascendingVectorSort(Vector objList, int column)
    {
        int something_done;
        int n = objList.size();
        do {
            something_done = 0;
            for (int i = 0; i < n - 1; i++) {
                Object element1 = ((Vector)objList.elementAt(i)).elementAt(column);
                Object element2 = ((Vector)objList.elementAt(i + 1)).elementAt(column);
                // if( element1 > element2 )
                if( (((Comparable)(element2)).compareTo(element1))>0 ) {
                    objList.setElementAt(((Vector)objList.elementAt(i + 1)), i);
                    objList.setElementAt(((Vector)objList.elementAt(i)), i + 1);
                    something_done=1;
                }
            }
            n--;
        } while(something_done==1 && n > 1);
    }

This is the output:

 re8622-505364.flv
0
 
LVL 9

Assisted Solution

by:oheil
oheil earned 500 total points
ID: 36813517
So, I would recommend:
<http://en.wikipedia.org/wiki/Quicksort>
implementing is straight foward.

Oli
0
 

Author Comment

by:re8622
ID: 36814463
Thank you.
0
 
LVL 100

Expert Comment

by:mlmcc
ID: 37068366
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Prime numbers are natural numbers greater than 1 that have only two divisors (the number itself and 1). By “divisible” we mean dividend % divisor = 0 (% indicates MODULAR. It gives the reminder of a division operation). We’ll follow multiple approac…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question