?
Solved

Java Collections.sort Vector

Posted on 2006-06-06
6
Medium Priority
?
7,004 Views
Last Modified: 2008-01-09
How do I use compareTo method in my class to sort a vector of objects of that class

for example in the main method I would have something like:

Vector myVector  = new Vector();

Vector.add(new MyClass(1) );
...

Collections.sort(myVector);


I've tried a few things but I keep getting this error:


Exception in thread "main" java.lang.ClassCastException: MyClass
      at java.util.Arrays.mergeSort(Unknown Source)
      at java.util.Arrays.mergeSort(Unknown Source)
      at java.util.Arrays.sort(Unknown Source)
      at java.util.Collections.sort(Unknown Source)
0
Comment
Question by:polkadot
6 Comments
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 300 total points
ID: 16840220
Your classes need to be Comparable or you need to use a custom Comparator
0
 
LVL 3

Accepted Solution

by:
runa_paathak earned 800 total points
ID: 16840240
Collections.sort(myVector);

sorts myVector "into ascending order, according to the natural ordering of its elements". JavaDocs add "All elements in the list must implement the Comparable interface. Furthermore, all elements in the list must be mutually comparable (that is, e1.compareTo(e2) must not throw a ClassCastException for any elements e1 and e2 in the list)."

So you must
1. have your class MyClass implement the Comparable interface
2. Make sure myVector contains only objects of type MyClass.
0
 
LVL 92

Assisted Solution

by:objects
objects earned 300 total points
ID: 16840289
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 300 total points
ID: 16840515
0
 
LVL 5

Assisted Solution

by:maclema
maclema earned 300 total points
ID: 16856905
try this: I am using getEmployeeID as an example. you can use whatever method you choose to do the comparison

        public static Comparator CMP_MyClass = new Comparator<MyClass>()
      {
            public int compare(MyClassobjA, MyClassobjB)
            {
                final int BEFORE = -1;
                final int AFTER = 1;
                  final int EQUAL = 0;
               
                if ( objA.getEmployeeID() < objB.getEmployeeID() )
                {
                  return BEFORE;
                }
                else if ( objA.getEmployeeID() == objB.getEmployeeID() )
                {
                  return EQUAL;
                }
                else
                {
                  return AFTER;
                }
            }
      };

Collections.sort(myVector, CMP_MyClass);
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16896903
:-)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month13 days, 21 hours left to enroll

807 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