Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 309
  • Last Modified:

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

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
re8622
Asked:
re8622
  • 3
  • 3
2 Solutions
 
oheilCommented:
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
 
re8622Author Commented:
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
 
oheilCommented:
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
Technology Partners: 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!

 
re8622Author Commented:
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
 
oheilCommented:
So, I would recommend:
<http://en.wikipedia.org/wiki/Quicksort>
implementing is straight foward.

Oli
0
 
re8622Author Commented:
Thank you.
0
 
mlmccCommented:
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

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!

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now