knox-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kris...@apache.org
Subject [knox] branch master updated: KNOX-2027 - Need a reverse order sort on a table based on a column. (#159)
Date Mon, 30 Sep 2019 22:49:58 GMT
This is an automated email from the ASF dual-hosted git repository.

krisden pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/knox.git


The following commit(s) were added to refs/heads/master by this push:
     new 34f2aa0  KNOX-2027 - Need a reverse order sort on a table based on a column. (#159)
34f2aa0 is described below

commit 34f2aa0917c7453d27d66c0389eaa43670c2c6fb
Author: Larry McCay IV <46705753+lmccay4@users.noreply.github.com>
AuthorDate: Mon Sep 30 18:49:53 2019 -0400

    KNOX-2027 - Need a reverse order sort on a table based on a column. (#159)
---
 .../org/apache/knox/gateway/shell/table/KnoxShellTable.java | 13 ++++++++++++-
 .../apache/knox/gateway/shell/table/KnoxShellTableTest.java | 10 ++++++++++
 2 files changed, 22 insertions(+), 1 deletion(-)

diff --git a/gateway-shell/src/main/java/org/apache/knox/gateway/shell/table/KnoxShellTable.java
b/gateway-shell/src/main/java/org/apache/knox/gateway/shell/table/KnoxShellTable.java
index 1fea1f9..04e5320 100644
--- a/gateway-shell/src/main/java/org/apache/knox/gateway/shell/table/KnoxShellTable.java
+++ b/gateway-shell/src/main/java/org/apache/knox/gateway/shell/table/KnoxShellTable.java
@@ -21,6 +21,8 @@ import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
 
+import javax.swing.SortOrder;
+
 import org.apache.knox.gateway.util.JsonUtils;
 
 
@@ -122,6 +124,10 @@ public class KnoxShellTable {
   }
 
   public KnoxShellTable sort(String colName) {
+    return sort(colName, SortOrder.ASCENDING);
+  }
+
+  public KnoxShellTable sort(String colName, SortOrder order) {
     KnoxShellTable table = new KnoxShellTable();
 
     Comparable<? extends Object> value;
@@ -131,7 +137,12 @@ public class KnoxShellTable {
       value = col.get(i);
       index.add(new RowIndex(value, i));
     }
-    Collections.sort(index);
+    if (SortOrder.ASCENDING.equals(order)) {
+      Collections.sort(index);
+    }
+    else {
+      Collections.sort(index, Collections.reverseOrder());
+    }
     table.headers = new ArrayList<String>(headers);
     for (RowIndex i : index) {
       table.rows.add(new ArrayList<Comparable<? extends Object>>(this.rows.get(i.index)));
diff --git a/gateway-shell/src/test/java/org/apache/knox/gateway/shell/table/KnoxShellTableTest.java
b/gateway-shell/src/test/java/org/apache/knox/gateway/shell/table/KnoxShellTableTest.java
index 3141afe..6b0c60c 100644
--- a/gateway-shell/src/test/java/org/apache/knox/gateway/shell/table/KnoxShellTableTest.java
+++ b/gateway-shell/src/test/java/org/apache/knox/gateway/shell/table/KnoxShellTableTest.java
@@ -34,6 +34,8 @@ import java.sql.ResultSet;
 import java.sql.ResultSetMetaData;
 import java.sql.Statement;
 
+import javax.swing.SortOrder;
+
 import org.apache.commons.io.FileUtils;
 import org.easymock.IAnswer;
 import org.junit.Test;
@@ -194,6 +196,14 @@ public class KnoxShellTableTest {
     KnoxShellTable table2 = table.sort("Column A");
     assertEquals(table2.getRows().get(0).get(0), "123");
     assertEquals(table2.getRows().get(1).get(0), "789");
+
+    table2 = table.sort("Column A", SortOrder.DESCENDING);
+    assertEquals(table2.getRows().get(0).get(0), "789");
+    assertEquals(table2.getRows().get(1).get(0), "123");
+
+    table2 = table.sort("Column B", SortOrder.DESCENDING);
+    assertEquals(table2.getRows().get(0).get(1), "456");
+    assertEquals(table2.getRows().get(1).get(1), "012");
   }
 
   @Test


Mime
View raw message