This is an automated email from the ASF dual-hosted git repository.

lmccay 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 b225050  KNOX-2308 - Add sortNumeric to KnoxShellTable for Cols that 
are numeric but values are String (#300)
b225050 is described below

commit b22505022b17388c099e419185409a35455f1827
Author: lmccay <lmc...@apache.org>
AuthorDate: Thu Mar 26 10:57:56 2020 -0400

    KNOX-2308 - Add sortNumeric to KnoxShellTable for Cols that are numeric but 
values are String (#300)
    
    Change-Id: I773e5ee9805347de36d5e4921a026f91b35c54b7
---
 .../apache/knox/gateway/shell/table/KnoxShellTable.java  | 16 +++++++++++++++-
 .../knox/gateway/shell/table/KnoxShellTableTest.java     | 14 ++++++++++++++
 2 files changed, 29 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 4c5368a..e630118 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
@@ -379,11 +379,25 @@ public class KnoxShellTable {
     return sort(colName, SortOrder.ASCENDING);
   }
 
+  public KnoxShellTable sortNumeric(String colName) {
+    double[] col = toDoubleArray(colName);
+    ArrayList<Comparable<? extends Object>> column = new ArrayList<>();
+    for(double v : col) {
+      column.add(v);
+   }
+    return sort(column, SortOrder.ASCENDING);
+  }
+
   public KnoxShellTable sort(String colName, SortOrder order) {
+    List<Comparable<? extends Object>> col = values(colName);
+    return sort(col, order);
+  }
+
+  public KnoxShellTable sort(List<Comparable<? extends Object>> col,
+      SortOrder order) {
     KnoxShellTable table = new KnoxShellTable();
 
     Comparable<? extends Object> value;
-    List<Comparable<? extends Object>> col = values(colName);
     List<RowIndex> index = new ArrayList<>();
     for (int i = 0; i < col.size(); i++) {
       value = col.get(i);
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 5644924..676bef6 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
@@ -284,6 +284,20 @@ public class KnoxShellTableTest {
   }
 
   @Test
+  public void testSortStringValuesNumerically() throws IOException {
+    KnoxShellTable table = new KnoxShellTable();
+
+    table.header("Column A").header("Column B").header("Column C");
+
+    table.row().value("2").value("012").value("844444444");
+    table.row().value("10").value("456").value("344444444");
+
+    KnoxShellTable table2 = table.sortNumeric("Column A");
+    assertEquals(table2.getRows().get(0).get(0), "2");
+    assertEquals(table2.getRows().get(1).get(0), "10");
+  }
+
+  @Test
   @SuppressWarnings({ "rawtypes", "unchecked" })
   public void testCells() throws IOException {
     KnoxShellTable table = new KnoxShellTable();

Reply via email to