/* package whatever; // don't place package name! */ import java.util.*; import java.lang.*; import java.io.*; /* Name of the class has to be "Main" only if the class is public. */ class Ideone { { data[0][0] = "Columbia"; data[0][1] = "Chile"; data[0][2] = "Asia"; data[0][3] = "US"; data[1][0] = "B216"; data[1][1] = "B217"; data[1][2] = "A442"; // data[1][3] = "N665"; data[2][0] = "This"; data[2][1] = "Is"; data[2][2] = "So"; data[2][3] = "Cool"; data[3][0] = "Holy"; data[3][1] = "Z"; data[3][2] = "AZZZ"; // Sort the String array and insert it in List list.add(s); } // Sort the list using custom comparator @Override // optional: condition to check for string length equality if (o1.length == o2.length) { for (int i = 0; i < o1.length; i++) { if (o1[i].equals(o2[i])) { // ** IMP ** // allows scanning through entire string array. continue; } // Find the first different strings in both arrays return o1[i].compareTo(o2[i]); } } else if (o1.length < o2.length) { // allow string arrays with lesser elements to appear first return -1; } else if (o1.length > o2.length) { return 1; } // When o1.length == o2.length and all strings are equal return 0; // no difference } }); // Printing the original array and our list } } }
Standard input is empty
[[Columbia, Chile, Asia, US], [B216, B217, A442], [This, Is, So, Cool], [Holy, Z, AZZZ]] [A442, B216, B217] [AZZZ, Holy, Z] [Asia, Chile, Columbia, US] [Cool, Is, So, This]