?
Solved

Sorting Items in a Vector

Posted on 2004-10-01
10
Medium Priority
?
242 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 1000 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

718 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