Repository: reanahub/reana-client

Issue:
cli: sorting by run number behaves correctly only with lowercase column name
In #619, we solved the `reana-client list` issue of incorrectly sorting the workflow list by run number, because the run number was considered as a string, and thus `2` came after `178`.
However, the fix only works when the column name is specified in lowercase: IOW, `reana-client list --sort run_number` works, but `reana-client list --sort RUN_NUMBER` does not. The other sorting keys appear to work fine in both uppercase and lowercase.

This is because in the case of the run number, the sorting key is overridden only when the column name passed by the user as an argument is lowercase:
https://github.com/reanahub/reana-client/blob/9c94abed31f522a1ff01bd64abd7975b4c675980/reana_client/cli/workflow.py#L273-L287

We can solve this quickly by:
- Changing the `get_sort_key` function so that it takes the column name from the `column_id` argument
- Changing the `get_sort_key` function so that it converts the column name to lowercase before comparing it to `run_number`

Discussion:

Patch:
diff --git a/CHANGES.rst b/CHANGES.rst
index 49748d6..6df40c9 100644
--- a/CHANGES.rst
+++ b/CHANGES.rst
@@ -1,6 +1,11 @@
 Changes
 =======
 
+Version 0.9.2 (UNRELEASED)
+--------------------------
+
+- Fixes ``list`` command to be case-insensitive when using the ``--sort`` flag to sort the workflow runs by a specific column name.
+
 Version 0.9.1 (2023-09-27)
 --------------------------
 
diff --git a/reana_client/cli/workflow.py b/reana_client/cli/workflow.py
index ac55e0e..f8fd978 100644
--- a/reana_client/cli/workflow.py
+++ b/reana_client/cli/workflow.py
@@ -109,7 +109,7 @@ def workflow_execution_group(ctx):
 @human_readable_or_raw_option
 @click.option(
     "--sort",
-    "sort_columm_name",
+    "sort_column_name",
     default="CREATED",
     help="Sort the output by specified column",
 )
@@ -164,7 +164,7 @@ def workflows_list(  # noqa: C901
     show_all,
     verbose,
     human_readable_or_raw,
-    sort_columm_name,
+    sort_column_name,
     page,
     size,
     filters,
@@ -269,17 +269,19 @@ def workflows_list(  # noqa: C901
                 row.append(value)
             data.append(row)
 
+        sort_column_id = 2
+        sort_column_name = sort_column_name.lower()
+        if sort_column_name in headers[type]:
+            sort_column_id = headers[type].index(sort_column_name)
+
         # Sort by given column, making sure that `None` is at the bottom of the list.
         def get_sort_key(x, column_id):
-            if sort_columm_name == "run_number":
+            if headers[type][column_id] == "run_number":
                 return list(map(int, x[column_id].split(".")))
-            elif sort_columm_name in workspace_size_header:
+            elif headers[type][column_id] in workspace_size_header:
                 return x[column_id]["raw"]
             return x[column_id] is not None, x[column_id]
 
-        sort_column_id = 2
-        if sort_columm_name.lower() in headers[type]:
-            sort_column_id = headers[type].index(sort_columm_name.lower())
         data = sorted(
             data,
             key=lambda x: get_sort_key(x, sort_column_id),


Test patch:
diff --git a/tests/test_cli_workflows.py b/tests/test_cli_workflows.py
index 248ab9b..624b862 100644
--- a/tests/test_cli_workflows.py
+++ b/tests/test_cli_workflows.py
@@ -118,7 +118,7 @@ def test_deleted_workflows(cli_options: List[str], expected_status_filter: List[
     "cli_options, expected_output",
     [
         (
-            ["list", "-t", "000000", "--sort", "run_number"],
+            ["list", "-t", "000000", "--sort", "Run_NUMber"],
             (
                 "mytest   15           2018-06-13T10:55:37   -                     -                     running\n"
                 "mytest   2            2018-06-13T09:55:35   -                     -                     running\n"