Good day! There is a problem for which it is necessary to sort a two-dimensional array in a certain order.

Suppose there is an array of string arrays:

{root, dir1, dir2, dir3, file1}: {root, dir2, file} {root, dir3, file} {root, dir1, dir3,file} {root, dir3, dir1, file} {root, dir1, dir2} {root, file} 

You need to get the following:

 {root, dir1, dir2, dir3, file1} {root, dir1, dir2} <--- 2 {root, dir1, dir3,file} <--- 3 {root, dir3, dir1, file} <--- 1 {root, dir3, file} {root, dir2, file} {root, file} 

That is, (I will try to explain in simple language, do not kick the explanation for much) with the first line we print the longest array, then the arrays that have common data with the first array (up to root value) are output. When these arrays end we look back the longest of the remaining (1) and in the same vein we derive as at the beginning and so on until the end .. Please also pay attention to line (2), its length is less than line (3), but it is in the right place, because dir2 is common, and in line (3) common is dir1 . In general, something like that ... Thank you in advance for any help!

  • The answer was on the surface !!! - Sergey Fokin

2 answers 2

In fact, if you look closely at the task, then the task is not about sorting a two-dimensional array, but the task of sorting a one-dimensional array (if you like an array of strings) with a specific comparator.

In this context, a comparator is a string comparison algorithm - as specified by the top-starter.

It remains only to implement this comparator — in Java terms, implement the java.util.Comparator or Comparable interface and Comparable it into sort()

In general, concentrate on the comparator, the rest is garbage.

    So you yourself have described the algorithm. What is the problem ? Two-dimensional array - an array of references to one-dimensional arrays. To change the strings it is enough to swap (swap) references to one-dimensional arrays. Find the string (array) that most closely resembles this is not a problem. The case of technology.