Solved

Sorting Items in a Vector

Posted on 2004-10-01
10
233 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 30

Expert Comment

by:mayankeagle
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
triangle challenge 4 77
mapAB Challlenge 35 85
java  and programming certification ? 4 65
Java - Why doesn't this JFrame work 3 17
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

706 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

15 Experts available now in Live!

Get 1:1 Help Now