Solved

Sorting Items in a Vector

Posted on 2004-10-01
10
236 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how do i compare an object based on two fields 6 53
jar file executable 12 51
web application structure 18 76
throw exception 21 43
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…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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 tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

786 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