Solved

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

Posted on 2011-09-26
8
301 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: 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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
post4 challenge 28 121
Controlling which port to download from 4 76
Choosing the right language for new project 8 68
Excel file not created as expected 7 110
This article will show, step by step, how to integrate R code into a R Sweave document
The purpose of this article is to demonstrate how we can use conditional statements using Python.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

734 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