Java – how to sort vectors?

In Java, I want to know how to sort vectors on a specific column. One vector is used as a row and one vector is used to save all row vectors, such as

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, such as column 2

thank you

Solution

You can write a comparator < vector >, compare two vector objects according to the second element, and use collections sort(List,Comparator).

But in the long run, if you get rid of the vector in vector construct and replace the internal vector with a custom class that represents the data you want it to represent Then you will write a comparator < < MyClass >, which will be easier to explain ("Oh, this comparator compares based on name", rather than "why does this comparator take the element at index 1 and compare? What does that mean?")

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>