There is an interface in which the method is declared.
void sort(List<String[]> rows, int columnIndex); You need to implement this interface in its class.
public class Task1Impl implements IStringRowsListSorter, Comparator<String[]>{ //ΡΠ΅Π°Π»ΠΈΠ·Π°ΡΠΈΡ Π΄ΠΎΠ»ΠΆΠ½Π° ΡΠ°Π±ΠΎΡΠ°ΡΡ, ΠΊΠ°ΠΊ singleton. Π΄Π°ΠΆΠ΅ ΠΏΡΠΈ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°Π½ΠΈΠΈ ΠΈΠ· Π½Π΅ΡΠΊΠΎΠ»ΡΠΊΠΈΡ
ΠΏΠΎΡΠΎΠΊΠΎΠ². public static final IStringRowsListSorter INSTANCE = new Task1Impl(); @Override public void sort(final List<String[]> rows, final int columnIndex) { //ΠΌΠΎΡ ΡΠ΅Π°Π»ΠΈΠ·Π°ΡΠΈΡ } The method takes as parameters:
rows - table. (List is, as I understand it, a list of arrays of type String! I understand, the table itself, for example, the result of the query)
columnIndex - the field number in this table (column index) by which you want to sort. In our structure it is String, it turns out.
I understand that sorting needs to be done using the Comparator. that is, the class must be redefined:
@Override public int compare(String[] o1, String[] o2) {} We basically have to sort the records in the tables, which we pass to the Comparator, but how can we explain to it what we need, sort by rows, because in the Comparator we cannot also pass columnIndex -
@Override public int compare(String[] o1, String[] o2, int columnIndex) { o1[columnIndex] < o2 [columnIndex] } To work with this interface, I created a test class, so Iβm asking you to see if you made the structure correctly for working with the array and its output through for each
public class TestIStringRowsListSorter { List<String[]> rows = new ArrayList<String[]>(); public List<String[]> getStruct(){ return rows; } public void init(){ String[] query1 = new String[3]; // ΠΌΠ°ΡΡΠΈΠ² ΡΡΡΠΎΠΊ - ΡΡΡΠΎΠΊΠ° ΡΠ°Π±Π»ΠΈΡΡ query1[0] = "Contact"; // 1Π° ΡΡΡΠΎΠΊΠ° - 1Π° Π·Π°ΠΏΠΈΡΡ (1ΠΎ ΠΏΠΎΠ»Π΅) query1[1] = "1"; // 1Π° ΡΡΡΠΎΠΊΠ° - 1Π° Π·Π°ΠΏΠΈΡΡ (1ΠΎ ΠΏΠΎΠ»Π΅) query1[2] = "Peter"; // 1Π° ΡΡΡΠΎΠΊΠ° - 1Π° Π·Π°ΠΏΠΈΡΡ (1ΠΎ ΠΏΠΎΠ»Π΅) String[] query2 = new String[3]; query2[0] = "Contact"; query2[1] = "2"; query2[2] = "Helga"; rows.add(query1); // Π΄ΠΎΠ±Π°Π²Π»ΡΠ΅ΠΌ Π² ΡΠ΅ΡΡΠΎΠ²ΡΠΉ Π‘ΠΏΠΈΡΠΎΠΊ ΠΈΠ· ΠΌΠ°ΡΡΠΈΠ²ΠΎΠ² ΡΡΡΠΎΠΊ 1 ΠΌΠ°ΡΡΠΈΠ² ΡΡΡΠΎΠΊ rows.add(query2); // Π΄ΠΎΠ±Π°Π²Π»ΡΠ΅ΠΌ Π² ΡΠ΅ΡΡΠΎΠ²ΡΠΉ Π‘ΠΏΠΈΡΠΎΠΊ ΠΈΠ· ΠΌΠ°ΡΡΠΈΠ²ΠΎΠ² ΡΡΡΠΎΠΊ 1 ΠΌΠ°ΡΡΠΈΠ² ΡΡΡΠΎΠΊ } public void showInitStruct(){ //ΡΠ°Π·ΠΎΠ±ΡΠ°ΡΡΡΡ ΠΊΠ°ΠΊ ΡΠ΅Π°Π»ΠΈΠ·ΠΎΠ²Π°Π½ ΠΠΠΠ«ΠΉ Ρ Π΄ΠΆΠ°Π²Π° 1.5 ΡΠΏΠΎΡΠΎΠ± ΠΏΠ΅ΡΠ΅Π±ΠΎΡΠ° for (String[] tmp: rows){ //Π΄Π»Ρ ΠΊΠ°ΠΆΠ΄ΠΎΠ³ΠΎ ΠΌΠ°ΡΡΠΈΠ²Π° ΡΡΡΠΎΠΊ Π² ΡΠΏΠΈΡΠΊΠ΅ System.out.println("String[] array number " + rows.indexOf(tmp)); for (String column : tmp) // Π΄Π»Ρ ΠΊΠ°ΠΆΠ΄ΠΎΠΉ ΡΡΡΠΎΠΊΠΈ Π² ΠΌΠ°ΡΡΠΈΠ²Π΅ ΡΡΡΠΎΠΊ { System.out.println(column); } } } public String goTest() { return "Sucefull"; } } Logic class:
TestIStringRowsListSorter t = new TestIStringRowsListSorter(); t.init(); t.showInitStruct(); Logic execution result:
String[] array number 0 Contact 1 Peter String[] array number 1 Contact 2 Helga The last 2 classes created to simplify the work. The question, in fact, is how to do the sorting using Java tools β implement the Comparator, and sort through the standard collection methods, and if so, how to do it?