简体   繁体   中英

How to sort a Vector of Vectors?

In Java I am wondering how to sort vectors of vectors on a particular column where one vector is used as a row and one vector is used to hold all the row vectors eg

 Vector row = new Vector();
    Vector main = new Vector();

    row.add("Column1");
    row.add("Column2");
    row.add("Column3");

    main.add(row);

Then sort the variables in one of the columns eg Column2 .

Thank you

You could write a Comparator<Vector> that compares two Vector objects based on their second element and use Collections.sort(List,Comparator) with that.

But in the long run you'll be much better off if you get rid of the Vector -in- Vector construct and replace the inner Vector with a custom class that represents the data that you want it to represent. Then you'd write a Comparator<MyClass> which would be much easier to interpret ("oh, this comparator compares based on the first name" instead of "why does this comparator take the element at index 1 and compare that? What does that mean?").

I guess you want to sort in 'main' rather than 'row':

Vector<String> row = new Vector<String>();
Vector<Vector<String>> main = new Vector<Vector<String>>();

Collections.sort(main, new Comparator<Vector<String>>(){
    @Override  public int compare(Vector<String> v1, Vector<String> v2) {
        return v1.get(1).compareTo(v2.get(1)); //If you order by 2nd element in row
}});

(why do people still use Vector and avoid generics? I should ask that question on SO... ;) )

Let me suggest a modern refactoring first:

List<List<String>> main = new ArrayList<List<String>>();
List<String> row = new ArrayList<String>();
row.add("Column1");
row.add("Column2");
row.add("Column3");
main.add(row);

Now we can look at Collections.sort(Comparator<T> comp) which will do the sorting of main . We just have to implement a Comparator class that is able to compare two rows according to our parameter - which is a certain column, in our case:

public class MyComparator implements Comparator<List<String>> {
  private int columnIndex = 0;
  public MyComparator(int columnIndex) {this.columnIndex = columnIndex;}

  @Override
  public int compare(List<String> thisRow, List<String> otherRow) {
    return thisRow.get(columnIndex).compareTo(otherRow.get(columnIndex));
  }
}

Use the comparator like this:

Collections.sort(main, new MyComparator(1));  // will sort according to "column2"

Note - this is an incomplete implementation, I don't check if the index values are valid and if all rows have the same size .. should be done in production code.

Vectors are maybe not the best representation of your table. Have a look at Glazed Lists .

Create a reusable Comparator that can be used to sort on any index in the Vector (or List or Array). The Column Comparator does this for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM