InfoLister: sort rows in --list-java-properties and --list-environment-variables v_0
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sun, 24 May 2015 19:17:50 +0200
branchv_0
changeset 201 d3db5a72a089
parent 200 2e351d7c26c4
child 202 01078e09b85b
InfoLister: sort rows in --list-java-properties and --list-environment-variables
java/sql-dk/src/info/globalcode/sql/dk/InfoLister.java
--- a/java/sql-dk/src/info/globalcode/sql/dk/InfoLister.java	Sun May 24 19:10:25 2015 +0200
+++ b/java/sql-dk/src/info/globalcode/sql/dk/InfoLister.java	Sun May 24 19:17:50 2015 +0200
@@ -39,6 +39,8 @@
 import java.sql.DriverPropertyInfo;
 import java.sql.SQLException;
 import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
 import java.util.EnumSet;
 import java.util.HashSet;
 import java.util.List;
@@ -125,7 +127,7 @@
 		for (Entry<Object, Object> e : System.getProperties().entrySet()) {
 			data.add(new Object[]{e.getKey(), e.getValue()});
 		}
-		printTable(formatter, header, "-- Java system properties", null, data);
+		printTable(formatter, header, "-- Java system properties", null, data, 0);
 	}
 
 	private void listEnvironmentVariables() throws FormatterException, ConfigurationException {
@@ -134,7 +136,7 @@
 		for (Entry<String, String> e : System.getenv().entrySet()) {
 			data.add(new Object[]{e.getKey(), e.getValue()});
 		}
-		printTable(formatter, header, "-- environment variables", null, data);
+		printTable(formatter, header, "-- environment variables", null, data, 0);
 	}
 
 	private void listFormatters() throws ConfigurationException, FormatterException {
@@ -426,8 +428,24 @@
 	}
 
 	private void printTable(Formatter formatter, ColumnsHeader header, String sql, List<Parameter> parameters, List<Object[]> data) throws ConfigurationException, FormatterException {
+		printTable(formatter, header, sql, parameters, data, null);
+	}
+
+	private void printTable(Formatter formatter, ColumnsHeader header, String sql, List<Parameter> parameters, List<Object[]> data, final Integer sortByColumn) throws ConfigurationException, FormatterException {
 		printHeader(formatter, header, sql, parameters);
 
+		if (sortByColumn != null) {
+			Collections.sort(data, new Comparator<Object[]>() {
+
+				@Override
+				public int compare(Object[] o1, Object[] o2) {
+					String s1 = String.valueOf(o1[sortByColumn]);
+					String s2 = String.valueOf(o2[sortByColumn]);
+					return s1.compareTo(s2);
+				}
+			});
+		}
+
 		for (Object[] row : data) {
 			printRow(formatter, row);
 		}