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?

    1 answer 1

    Inside your sort create an anonymous comparator String[] , in which your columnIndex parameter will be visible inside (I recommend to get acquainted with the information about the scope in the inner classes):

     public void sort(final List<String[]> rows, final int columnIndex) { Collections.sort(rows, new Comparator<String[]>() { @Override public int compare(String[] o1, String[] o2) { return o1[columnIndex].compareTo(o2[columnIndex]); } }); } 
    • And why did I not think about anonymous classes before! Tell me, I understood correctly that the compareTo method in 'o1[columnIndex].compareTo(o2[columnIndex]); This is the String method, i.e. Does String implement the Comparable interface, and to calculate the result of comparing arrays of strings, we use the result of computing strings by index? - Mr.Ki.D.
    • @ Mr.Ki.D., Yes, that's right. - iksuy