You can use a static field to keep track of the sorted column (sortField
). Each time you display the data, grab a the sorting instance with the current field.
import java.util.*;
import java.util.stream.*;
public class JavaApplication45 {
static String[] fields = { "City", "Lat", "Long", "District", "Postcode" };
static String[][] data;
static String[][] zip;
static String sortField;
static Comparator<String[]> currentComparator;
public static void main(String[] args) {
display();
setSortField("City"); // Change the sorting...
display();
}
private static void setSortField(String newSortField) {
if (sortField != newSortField) {
sortField = newSortField;
currentComparator = generateComparator();
}
}
private static void display() {
Collection<String[]> lines = getCollection();
if (currentComparator != null) {
lines = lines.stream().sorted(currentComparator).collect(Collectors.toList());
}
System.out.println(Arrays.stream(fields).collect(Collectors.joining("\t")));
System.out.println("-".repeat(60));
for (String[] line : lines) {
System.out.println(Stream.of(line).collect(Collectors.joining("\t")));
}
System.out.println();
}
private static Collection<String[]> getCollection() {
Collection<String[]> lines = new ArrayList<>();
for (int row = 0; row < data.length; row++) {
String[] cols = new String[data[row].length + 1];
for (int col = 0; col < data[row].length; col++) {
cols[col] = data[row][col];
}
cols[data[row].length] = zip[row][1];
lines.add(cols);
}
return lines;
}
private static Comparator<String[]> generateComparator() {
int sortIndex = Arrays.asList(fields).indexOf(sortField);
return new Comparator<String[]>() {
@Override
public int compare(String[] left, String[] right) {
return sortIndex != -1 ? left[sortIndex].compareTo(right[sortIndex]) : 0;
}
};
}
static {
data = new String[8][4];
data[0][0] = "Curepipe";
data[0][1] = "-20.3162";
data[0][2] = "57.5166";
data[0][3] = "Plaine Wilhems";
data[1][0] = "Ebene";
data[1][1] = "-20.2456";
data[1][2] = "57.4842";
data[1][3] = "Plaine Wilhems ";
data[2][0] = "Rose Hill";
data[2][1] = "-20.2456";
data[2][2] = "57.4719";
data[2][3] = "Plaine Wilhems";
data[3][0] = "Beau Bassin";
data[3][1] = "-20.2276";
data[3][2] = "57.4681";
data[3][3] = "Plaine Wilhems";
data[4][0] = "Quatre Bornes";
data[4][1] = "-20.2654";
data[4][2] = "57.4778";
data[4][3] = "Plaine Wilhems";
data[5][0] = "Vacoas";
data[5][1] = "-20.2981";
data[5][2] = "57.4783";
data[5][3] = "Plaine Wilhems";
data[6][0] = "Mahebourg";
data[6][1] = "20.4081";
data[6][2] = "57.7000";
data[6][3] = "Grand Port";
data[7][0] = "Goodlands";
data[7][1] = "-20.0350";
data[7][2] = "57.6431";
data[7][3] = "Rivere du Rempart";
zip = new String[8][2];
zip[0][0] = "Curepipe";
zip[0][1] = "74415";
zip[1][0] = "Goodlands";
zip[1][1] = "30401";
zip[2][0] = "Rose Hill";
zip[2][1] = "71360";
zip[3][0] = "Beau Bassin";
zip[3][1] = "71401";
zip[4][0] = "Vacoas";
zip[4][1] = "73304";
zip[5][0] = "Mahebourg";
zip[5][1] = "50810";
zip[6][0] = "Quatre Bornes";
zip[6][1] = "72249";
zip[7][0] = "Ebene";
zip[7][1] = "72201";
}
}
Output
City Lat Long District Postcode
------------------------------------------------------------
Curepipe -20.3162 57.5166 Plaine Wilhems 74415
Ebene -20.2456 57.4842 Plaine Wilhems 30401
Rose Hill -20.2456 57.4719 Plaine Wilhems 71360
Beau Bassin -20.2276 57.4681 Plaine Wilhems 71401
Quatre Bornes -20.2654 57.4778 Plaine Wilhems 73304
Vacoas -20.2981 57.4783 Plaine Wilhems 50810
Mahebourg 20.4081 57.7000 Grand Port 72249
Goodlands -20.0350 57.6431 Rivere du Rempart 72201
City Lat Long District Postcode
------------------------------------------------------------
Beau Bassin -20.2276 57.4681 Plaine Wilhems 71401
Curepipe -20.3162 57.5166 Plaine Wilhems 74415
Ebene -20.2456 57.4842 Plaine Wilhems 30401
Goodlands -20.0350 57.6431 Rivere du Rempart 72201
Mahebourg 20.4081 57.7000 Grand Port 72249
Quatre Bornes -20.2654 57.4778 Plaine Wilhems 73304
Rose Hill -20.2456 57.4719 Plaine Wilhems 71360
Vacoas -20.2981 57.4783 Plaine Wilhems 50810