Repository: spyder-ide/spyder

Issue:
Variable explorer: Sorting not working
<!--- **PLEASE READ:** When submitting here, please ensure you've completed the following checklist and checked the boxes to confirm. Issue reports without it may be closed. Thanks! --->

### Issue Report Checklist

* [x] Searched the [issues page](https://github.com/spyder-ide/spyder/issues?q=is%3Aissue) for similar reports
* [x] Read the relevant sections of the [Spyder Troubleshooting Guide](https://github.com/syder-ide/spyder/wiki/Troubleshooting-Guide-and-FAQ) and followed its advice
* [x] Reproduced the issue after updating with ``conda update spyder`` (or ``pip``, if not using Anaconda)
* [x] Tried basic troubleshooting (if a bug/error)
    * [x] Restarted Spyder
    * [ ] Reset preferences with ``spyder --reset``
    * [x] Reinstalled the latest version of [Anaconda](https://www.anaconda.com/download/)
    * [ ] Tried the other applicable steps from the Troubleshooting Guide
* [x] Completed the **Problem Description**, **Steps to Reproduce** and **Version** sections below


## Problem Description
Sorting is not working in Variable explorer on latest master. Nothing happens when I click the column headings, so it is always sorted in ascending order on the first column.


### What steps reproduce the problem?

1. Click on column headings


### What is the expected output? What do you see instead?
Variables are sorted (as in Spyder 3).


## Versions
<!--- You can get this information from Help > About Spyder...
or (if Spyder won't launch) the "conda list" command
from the Anaconda Prompt/Terminal/command line. --->

* Spyder version: 4.0.0.dev0
* Python version: 3.7.3 64-bit
* Qt version: 5.9.6
* PyQt version: 5.9.2
* Operating System name/version: Windows 10

### Dependencies
<!--- Please go to the menu entry Help > Dependencies,
press the Copy to clipboard button and paste below --->

```
pygments >=2.0    :  2.4.2 (OK)
qdarkstyle >=2.6.4:  2.7 (OK)
sphinx >=0.6.6    :  2.1.2 (OK)
pyls >=0.27.0     :  0.27.0 (OK)
nbconvert >=4.0   :  5.5.0 (OK)
pandas >=0.13.1   :  0.24.2 (OK)
numpy >=1.7       :  1.16.4 (OK)
sympy >=0.7.3     :  1.4 (OK)
cython >=0.21     :  0.29.11 (OK)
qtconsole >=4.5.0 :  4.5.1 (OK)
IPython >=4.0     :  7.5.0 (OK)
matplotlib >=2.0.0:  3.1.0 (OK)
pylint >=0.25     :  2.3.1 (OK)
```



Discussion:
Thanks for the report. @dalthviz could you take a look? it might be related to some of your earlier work.

Resolving patch:
diff --git a/spyder/plugins/variableexplorer/widgets/collectionseditor.py b/spyder/plugins/variableexplorer/widgets/collectionseditor.py
--- a/spyder/plugins/variableexplorer/widgets/collectionseditor.py
+++ b/spyder/plugins/variableexplorer/widgets/collectionseditor.py
@@ -723,6 +723,9 @@ def __init__(self, parent=None):
         super(BaseHeaderView, self).__init__(Qt.Horizontal, parent)
         self._handle_section_is_pressed = False
         self.sectionResized.connect(self.sectionResizeEvent)
+        # Needed to enable sorting by column
+        # See spyder-ide/spyder#9835
+        self.setSectionsClickable(True)
 
     def mousePressEvent(self, e):
         self._handle_section_is_pressed = (self.cursor().shape() ==


Test patch:
diff --git a/spyder/plugins/variableexplorer/widgets/tests/test_namespacebrowser.py b/spyder/plugins/variableexplorer/widgets/tests/test_namespacebrowser.py
--- a/spyder/plugins/variableexplorer/widgets/tests/test_namespacebrowser.py
+++ b/spyder/plugins/variableexplorer/widgets/tests/test_namespacebrowser.py
@@ -15,9 +15,11 @@
 
 # Third party imports
 import pytest
+from qtpy.QtCore import Qt, QPoint
 
 # Local imports
 from spyder.plugins.variableexplorer.widgets.namespacebrowser import NamespaceBrowser
+from spyder.plugins.variableexplorer.widgets.tests.test_collectioneditor import data_table
 
 def test_setup_sets_dataframe_format(qtbot):
     browser = NamespaceBrowser(None)
@@ -48,5 +50,48 @@ def test_automatic_column_width(qtbot):
     assert browser.editor.columnWidth(0) == 100  # Automatic col width is off
 
 
+def test_sort_by_column(qtbot):
+    """
+    Test that clicking the header view the namespacebrowser is sorted.
+    Regression test for spyder-ide/spyder#9835 .
+    """
+    browser = NamespaceBrowser(None)
+    qtbot.addWidget(browser)
+    browser.set_shellwidget(Mock())
+    browser.setup(exclude_private=True, exclude_uppercase=True,
+                  exclude_capitalized=True, exclude_unsupported=True,
+                  minmax=False)
+    browser.set_data(
+        {'a_variable':
+            {'type': 'int', 'size': 1, 'color': '#0000ff', 'view': '1'},
+         'b_variable':
+            {'type': 'int', 'size': 1, 'color': '#0000ff', 'view': '2'}})
+
+    header = browser.editor.horizontalHeader()
+
+    # Check header is clickable
+    assert header.sectionsClickable()
+
+    model = browser.editor.model
+
+    # Base check of the model
+    assert model.rowCount() == 2
+    assert model.columnCount() == 4
+    assert data_table(model, 2, 4) == [['a_variable', 'b_variable'],
+                                       ['int', 'int'],
+                                       ['1', '1'],
+                                       ['1', '2']]
+
+    with qtbot.waitSignal(header.sectionClicked):
+        browser.show()
+        qtbot.mouseClick(header.viewport(), Qt.LeftButton, pos=QPoint(1, 1))
+
+    # Check sort effect
+    assert data_table(model, 2, 4) == [['b_variable', 'a_variable'],
+                                       ['int', 'int'],
+                                       ['1', '1'],
+                                       ['2', '1']]
+
+
 if __name__ == "__main__":
     pytest.main()