aspose file tools*
The moose likes Java in General and the fly likes Sorting a Vector by variable in the Object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Sorting a Vector by variable in the Object" Watch "Sorting a Vector by variable in the Object" New topic
Author

Sorting a Vector by variable in the Object

Rich Wask
Greenhorn

Joined: Sep 05, 2000
Posts: 3
I am trying to sort a Vector of objects by "variables" defined globaly, that are set in the object.
Example:
class XXX {
String field1;
String field2;
String field3;
}
///////////////////////////////////////////////
import XXX;
class TestXXX {
public static void main( String args[] ) {
Vector B = new Vector();
XXX A = new XXX();
XXX Q = new XXX();
XXX Z = new XXX();
Z.field1 = "field1Z"
B.add(Z);
Q.field1 = "field1Q"
B.add(Q);
A.field1 = "field1A"
B.add(A);
}
}
At this point I have a Vector(B) of XXX objects(Z,Q,A). All the objects have "field1" populated with a string value. I would like to sort this vector by the "field1" in each of the objects.
Before sort:
B.elementAt(0) would be object Z, B.elementAt(1) would be object Q, etc.
After sort:
B.elementAt(0) should now be object A, B.elementAt(1) should be object Q, etc.
A code example reply would be great.
Stephanie Grasson
Ranch Hand

Joined: Jun 14, 2000
Posts: 347
Rich,
This is sort of a brute force example, not very elegant but I believe it solves your problem. Maybe you can use it until you think of something better:

Hope this helps.
Stephanie
P.S.
For the sake of keeping things simple, I put both classes in one file. Obviously, you could easily substitute your import statement for class XXX.
Rich Wask
Greenhorn

Joined: Sep 05, 2000
Posts: 3
Thanks Stephanie and Jesse
Both your answers were a great help.
I found an excerpt from the book "Core Java 2, volume 2", by Cay S, Horstmann and Gary Cornell, at the java.sun web site. The address is: http://developer.java.sun.com/developer/Books/corejava/index.html
I need to finish reading the pages to fully understand Collections, but I read enough information and examples on Collections to get what I needed.
Thanks Again.
Maybe I should have looked there first.
I think I am getting out of hand with the Smilie faces.
Originally posted by Rich Wask:
I am trying to sort a Vector of objects by "variables" defined globaly, that are set in the object.
Example:
class XXX {
String field1;
String field2;
String field3;
}
///////////////////////////////////////////////
import XXX;
class TestXXX {
public static void main( String args[] ) {
Vector B = new Vector();
XXX A = new XXX();
XXX Q = new XXX();
XXX Z = new XXX();
Z.field1 = "field1Z"
B.add(Z);
Q.field1 = "field1Q"
B.add(Q);
A.field1 = "field1A"
B.add(A);
}
}
At this point I have a Vector(B) of XXX objects(Z,Q,A). All the objects have "field1" populated with a string value. I would like to sort this vector by the "field1" in each of the objects.
Before sort:
B.elementAt(0) would be object Z, B.elementAt(1) would be object Q, etc.
After sort:
B.elementAt(0) should now be object A, B.elementAt(1) should be object Q, etc.
A code example reply would be great.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sorting a Vector by variable in the Object