Solved

Sorting Items in a Vector

Posted on 2004-10-01
10
235 Views
Last Modified: 2010-03-31
Can anyone point me to some source code that shows examples of sorting items within a Vector.
Is this possible? or do the items need to be sorted first then inserted into the Vector? The Vector will
be updated periodically and must be sorted, with no duplicates added.

thanks
0
Comment
Question by:hugoitt
  • 2
  • 2
  • 2
  • +4
10 Comments
 
LVL 19

Accepted Solution

by:
ramazanyich earned 250 total points
ID: 12203625
0
 
LVL 19

Expert Comment

by:ramazanyich
ID: 12203687
In general Objects in your Vector has to implement the Comparable interface
Then you can use
java.util.Collections.sort(myVector);

Comparable interface has only one method:
public int compareTo(Object o)
That method:
"Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object."

If your Vector contains BigDecimal, BigInteger, Byte, ByteBuffer, Character, CharBuffer, Charset, CollationKey, Date, Double, DoubleBuffer, File, Float, FloatBuffer, IntBuffer, Integer, Long, LongBuffer, ObjectStreamField, Short, ShortBuffer, String or URI  objects you don't have to do anything because those classes implements Comparable interface.
0
 
LVL 4

Expert Comment

by:funnyveryfunny
ID: 12204243
adding to what ramazanyich's comment, you can check for duplicates by using Vector.contains() if necessary override it to your own implementation.
0
 
LVL 92

Expert Comment

by:objects
ID: 12204703
0
 
LVL 1

Expert Comment

by:achilka
ID: 12206349
First Use  java.util.TreeSet  and add elements which takes care of any duplicates and sorting in ascending order by default

TreeSet tree = new TreeSet();

tree.add(element); //Adds the specified element to this set if it is not already present.
//this will eleminate any duplicates

//if you need a vector of the above sorted unique elements user

Vector v = new Vector();
v.addAll(tree);

Cheers!!!
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 30

Expert Comment

by:Mayank S
ID: 12206500
0
 
LVL 5

Expert Comment

by:Naeemg
ID: 12216345
hello man, try this code, u'll get the sorted list.

    java.util.Vector thisVector = new java.util.Vector();
    thisVector.addElement("B");
    thisVector.addElement("C");
    thisVector.addElement("A");
    thisVector.addElement("D");
    java.util.Collections.sort(thisVector);
    for(int i = 0; i < thisVector.size(); i++)
      System.out.println(thisVector.get(i));
 

Bye
Naeem Shehzad Ghuman
0
 
LVL 5

Expert Comment

by:Naeemg
ID: 12251566
Wan't my code helpful to u?
0
 

Author Comment

by:hugoitt
ID: 12288769
Naeemg,
I've been going through all the example code and trying to get other things accomplished, your sample code is called out in those examples listed above. It does not work for me, I'm sortiing numbers from 1-10000 and collections.sort does not sort the values in order.
0
 

Author Comment

by:hugoitt
ID: 12308503
thanks for the responses
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Android studio getdrawable(int) is deprecated 4 43
groupSumClump challenge 9 103
Groovy:unable to resolve class error 2 70
print map entry 34 55
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
The viewer will learn how to implement Singleton Design Pattern in Java.

867 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now