Sorting a Vector of Vectors

I would like to sort a Vector of Vectors based on one element in the enclosed vectors.

Similar to sorting the rows of a JTable based on one column, but in this case there is no JTable.

Vector one =new Vector();

one.addElement("al" );

one.addElement("plumber" );

one.addElement("25,000" );

Vector two =new Vector();

two.addElement("bill" );

two.addElement("engineer" );

two.addElement("65,000" );

Vector three =new Vector();

three.addElement("cathy" );

three.addElement("marketing" );

three.addElement("100,000" );

Vector workers =new Vector();

workers.addElement("one" );

workers.addElement("two" );

workers.addElement("three" );

Is there any way to sort the "workers" vector based on the "name" vector?

i.e.: (Vector)workers.elementAt(x).elementAt(0);

Many thanks in advance

[1350 byte] By [java_Huha] at [2007-10-3 2:52:17]
# 1

You could wrap the inside Vector inside of another class, implement comparable with that class, and construct the comparison in such a way that it will always compare the first value contained in the first vector against the first value contained in the second vector.

However, what you should do is make a class to contain the info, and have it be a Vector of that class, instead of a Vector of Vectors. This way, you can establish the comparison for that class to be the comparison of the name instance variable, which would be MUCH more understandable later on.

technodolta at 2007-7-14 20:41:15 > top of Java-index,Java Essentials,Java Programming...
# 2
It looks like the "worker" should be its own class.Then you would sort a List of Workers.
zadoka at 2007-7-14 20:41:15 > top of Java-index,Java Essentials,Java Programming...
# 3

You can do this sort of thing using a java.util.Comparator.

But some BIG notes:

1) workers is NOT a Vector of Vectors. It's a Vector of Strings, the way you've written it.

You probably meant to do:

Vector workers = new Vector();

workers.addElement(one);

workers.addElement(two);

workers.addElement(three );

2) It's really crappy design to have a Vector or an array or a similar positional data structure and then have different fields occupying different positions, and then identifying the fields by their positions. What you should do is define a Worker class.

paulcwa at 2007-7-14 20:41:15 > top of Java-index,Java Essentials,Java Programming...
# 4

> It looks like the "worker" should be its own class. Then you would sort a List of Workers.

That would be the best design. Then you do the sort like this:

http://forum.java.sun.com/thread.jspa?forumID=31&threadID=598401

However, a JTable is able to do a sort on a Vector of Vectors so its not an uncommon problem. Here is solution for stated problem:

import java.util.Comparator;

import java.util.List;

/**

* A comparator to sort Lists of data based on a column within each List

*/

public class ListComparator implements Comparator

{

private int column;

private boolean ascending;

ListComparator(int column, boolean ascending)

{

this.column = column;

this.ascending = ascending;

}

public int compare(Object a, Object b)

{

List v1 = (List)a;

List v2 = (List)b;

Object o1 = v1.get(column);

Object o2 = v2.get(column);

// Treat empty strings like nulls

if (o1 instanceof String && ((String)o1).length() == 0)

{

o1 = null;

}

if (o2 instanceof String && ((String)o2).length() == 0)

{

o2 = null;

}

// Sort nulls so they appear last, regardless of sort order

if (o1 == null && o2 == null) return 0;

if (o1 == null) return 1;

if (o2 == null) return -1;

// Compare objects

if (o1 instanceof Comparable)

{

if (ascending)

{

return ((Comparable)o1).compareTo(o2);

}

else

{

return ((Comparable)o2).compareTo(o1);

}

}

else // Compare as a String

{

if (ascending)

{

return o1.toString().compareTo(o2.toString());

}

else

{

return o2.toString().compareTo(o1.toString());

}

}

}

}

To invoke the sort you would just do:

Collections.sort(vectorOfVectors, new ListComparator(theColumn, ascending));

camickra at 2007-7-14 20:41:15 > top of Java-index,Java Essentials,Java Programming...