Solved

Sorting Items in a Vector

Posted on 2004-10-01
10
238 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
[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
  • 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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
 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

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…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

752 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