I wrote code that fills a two-dimensional array with data from a file.
public static Collection getBrowser() {
return Arrays.asList(readFromFile("arr.txt"));
}
private static Object[][] readFromFile(String filePath) {
try {
InputStream inputStream = new FileInputStream(filePath);
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
StringBuilder string = new StringBuilder();
while (reader.ready()) {
string.append(reader.readLine());
string.append(System.getProperty("line.separator"));
}
reader.close();
String[] lineSplit = string.toString().split(System.getProperty("line.separator"));
Object[][] result = new Object[lineSplit.length][];
for (int i = 0; i < lineSplit.length; i++) {
String line = lineSplit[i];
String[] itemSplit = line.split(",");
result[i] = new Object[itemSplit.length];
for (int j = 0; j < itemSplit.length; j++) {
if (j == 0) {
result[i][j] = Integer.valueOf(itemSplit[j]);
} else {
result[i][j] = itemSplit[j];
}
System.out.print(result[i][j]+" ");
}
//System.out.println();
}
return result;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
Example file
1000, a, a, x
1000, x, x, y
1200, c, c, y
Tell me please, how can I sort this array by the last value, by sweeping each line into a separate array
Arr1
1000, a, a, x
Arr2
1000, x, x, y
1200, c, c, y