Repository: spyder-ide/spyder

Issue:
Dictionary has keys and values mixed up
When opening a dictionary of dataframes from the Variable explorer, the sizes and values do not match the actual data. Specifically, when I start sorting the keys, one can see that the sizes are not sorted correctly:

![sort1](https://user-images.githubusercontent.com/38217520/98297893-6ccf5200-1fb5-11eb-9e1d-ce2aefa97e33.gif)

![sort2](https://user-images.githubusercontent.com/38217520/98299762-7f975600-1fb8-11eb-8742-70fbd475d2b6.gif)


I get this problem even before "sorting" the dictionary, because when opening the dictionary the keys by default are displayed alphabetically and not in the actual insertion order.

However, I do not have this problem with dicts of small dfs. E.g., sorting this one works perfectly fine:

```
directors = {}
directors["A"] = pd.DataFrame({"A": "joe","A2": "biden"}, index=[0])
directors["B"] = pd.DataFrame({"B": "don","B2": "trump"}, index=[0])
directors["C"] = pd.DataFrame({"C": "anderson","C2": "cooper"}, index=[0])
directors["D"] = pd.DataFrame({"D": "katie","D2": "hobbs"}, index=[0])
```

* Spyder version: 4.1.5
* Python version: 3.7.7 64-bit
* Qt version: 5.12.8
* PyQt version: 5.12.3
* Operating System name/version: Windows 10



Discussion:
Hey @skalierer, thanks for reporting. I think this was already fixed by @skjerns and it'll be available in our 4.2.0 version, to be available in the net few days.

@skjerns, could you confirm that?
this should be fixed indeed, unless this issue is different from #13733  which I cannot check without code
Apologies for not having found [#13733](https://github.com/spyder-ide/spyder/issues/13733) myself! This looks like the fix I was looking for. Thank you for your efforts!
I'm currently using 4.2.0 and see a similar issue.

Steps to reproduce:
1. Create a dict with lists as values. An illustrative example is:
`var = {'a_len=4': [1,2,3,4], 'b_len=8': [1,2,3,4,5,6,7,8], 'c_len=5': [1,2,3,4,5]}`
2. View the variable in the variable explorer. Click on "Size" twice (to sort increasing and then decreasing). On the second click only the size column is reordered. Thus the columns go out of sync.

As the problem only ocurred when sorting in the reverse direction, I quickly tracked it down to the line
https://github.com/spyder-ide/spyder/blob/436290ba71bb16c5928676467a358b70f4f38c10/spyder/plugins/variableexplorer/widgets/collectionseditor.py#L288
It is missing a keyword argument "reverse=reverse". (Verified by editing my local files.)

I could make a pull request with this change. Would branch 4.x be the right place to put it?

Hey @hengin, thanks for tracking that problem down! A pull request against 4.x is the right thing to do too. Thanks for your help! 

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
@@ -285,7 +285,9 @@ def all_string(listlike):
             except:
                 pass
         elif column == 2:
-            self.keys[:self.rows_loaded] = sort_against(self.keys, self.sizes)
+            self.keys[:self.rows_loaded] = sort_against(self.keys,
+                                                        self.sizes,
+                                                        reverse=reverse)
             self.types = sort_against(self.types, self.sizes, reverse=reverse)
             try:
                 self.sizes.sort(reverse=reverse)


Test patch: