Repository: spyder-ide/spyder

Issue:
Variable explorer sorting by Size
```python
File "/Applications/Spyder.app/Contents/Resources/lib/python3.9/spyder/plugins/variableexplorer/widgets/collectionseditor.py", line 1708, in lessThan
    return leftData < rightData
TypeError: '<' not supported between instances of 'int' and 'str'
Traceback (most recent call last):
  File "/Applications/Spyder.app/Contents/Resources/lib/python3.9/spyder/plugins/variableexplorer/widgets/collectionseditor.py", line 1708, in lessThan
    return leftData < rightData
TypeError: '<' not supported between instances of 'str' and 'int'
Traceback (most recent call last):
  File "/Applications/Spyder.app/Contents/Resources/lib/python3.9/spyder/plugins/variableexplorer/widgets/collectionseditor.py", line 1708, in lessThan
    return leftData < rightData
TypeError: '<' not supported between instances of 'int' and 'str'
Traceback (most recent call last):
  File "/Applications/Spyder.app/Contents/Resources/lib/python3.9/spyder/plugins/variableexplorer/widgets/collectionseditor.py", line 1708, in lessThan
    return leftData < rightData
TypeError: '<' not supported between instances of 'str' and 'int'
Traceback (most recent call last):
  File "/Applications/Spyder.app/Contents/Resources/lib/python3.9/spyder/plugins/variableexplorer/widgets/collectionseditor.py", line 1708, in lessThan
    return leftData < rightData
TypeError: '<' not supported between instances of 'str' and 'int'
Traceback (most recent call last):
  File "/Applications/Spyder.app/Contents/Resources/lib/python3.9/spyder/plugins/variableexplorer/widgets/collectionseditor.py", line 1708, in lessThan
    return leftData < rightData
TypeError: '<' not supported between instances of 'str' and 'int'
Traceback (most recent call last):
  File "/Applications/Spyder.app/Contents/Resources/lib/python3.9/spyder/plugins/variableexplorer/widgets/collectionseditor.py", line 1708, in lessThan
    return leftData < rightData
TypeError: '<' not supported between instances of 'int' and 'str'
Traceback (most recent call last):
  File "/Applications/Spyder.app/Contents/Resources/lib/python3.9/spyder/plugins/variableexplorer/widgets/collectionseditor.py", line 1708, in lessThan
    return leftData < rightData
TypeError: '<' not supported between instances of 'str' and 'int'
Traceback (most recent call last):
  File "/Applications/Spyder.app/Contents/Resources/lib/python3.9/spyder/plugins/variableexplorer/widgets/collectionseditor.py", line 1708, in lessThan
    return leftData < rightData
TypeError: '<' not supported between instances of 'str' and 'int'
Traceback (most recent call last):
  File "/Applications/Spyder.app/Contents/Resources/lib/python3.9/spyder/plugins/variableexplorer/widgets/collectionseditor.py", line 1708, in lessThan
    return leftData < rightData
TypeError: '<' not supported between instances of 'str' and 'int'
Traceback (most recent call last):
  File "/Applications/Spyder.app/Contents/Resources/lib/python3.9/spyder/plugins/variableexplorer/widgets/collectionseditor.py", line 1708, in lessThan
    return leftData < rightData
TypeError: '<' not supported between instances of 'str' and 'int'
Traceback (most recent call last):
  File "/Applications/Spyder.app/Contents/Resources/lib/python3.9/spyder/plugins/variableexplorer/widgets/collectionseditor.py", line 1708, in lessThan
    return leftData < rightData
TypeError: '<' not supported between instances of 'int' and 'str'
Traceback (most recent call last):
  File "/Applications/Spyder.app/Contents/Resources/lib/python3.9/spyder/plugins/variableexplorer/widgets/collectionseditor.py", line 1708, in lessThan
    return leftData < rightData
TypeError: '<' not supported between instances of 'int' and 'str'
Traceback (most recent call last):
  File "/Applications/Spyder.app/Contents/Resources/lib/python3.9/spyder/plugins/variableexplorer/widgets/collectionseditor.py", line 1708, in lessThan
    return leftData < rightData
TypeError: '<' not supported between instances of 'int' and 'str'
Traceback (most recent call last):
  File "/Applications/Spyder.app/Contents/Resources/lib/python3.9/spyder/plugins/variableexplorer/widgets/collectionseditor.py", line 1708, in lessThan
    return leftData < rightData
TypeError: '<' not supported between instances of 'int' and 'str'
Traceback (most recent call last):
  File "/Applications/Spyder.app/Contents/Resources/lib/python3.9/spyder/plugins/variableexplorer/widgets/collectionseditor.py", line 1708, in lessThan
    return leftData < rightData
TypeError: '<' not supported between instances of 'int' and 'str'
Traceback (most recent call last):
  File "/Applications/Spyder.app/Contents/Resources/lib/python3.9/spyder/plugins/variableexplorer/widgets/collectionseditor.py", line 1708, in lessThan
    return leftData < rightData
TypeError: '<' not supported between instances of 'int' and 'str'
Traceback (most recent call last):
  File "/Applications/Spyder.app/Contents/Resources/lib/python3.9/spyder/plugins/variableexplorer/widgets/collectionseditor.py", line 1708, in lessThan
    return leftData < rightData
TypeError: '<' not supported between instances of 'int' and 'str'
Traceback (most recent call last):
  File "/Applications/Spyder.app/Contents/Resources/lib/python3.9/spyder/plugins/variableexplorer/widgets/collectionseditor.py", line 1708, in lessThan
    return leftData < rightData
TypeError: '<' not supported between instances of 'int' and 'str'
Traceback (most recent call last):
  File "/Applications/Spyder.app/Contents/Resources/lib/python3.9/spyder/plugins/variableexplorer/widgets/collectionseditor.py", line 1708, in lessThan
    return leftData < rightData
TypeError: '<' not supported between instances of 'int' and 'str'
Traceback (most recent call last):
  File "/Applications/Spyder.app/Contents/Resources/lib/python3.9/spyder/plugins/variableexplorer/widgets/collectionseditor.py", line 1708, in lessThan
    return leftData < rightData
TypeError: '<' not supported between instances of 'int' and 'str'
Traceback (most recent call last):
  File "/Applications/Spyder.app/Contents/Resources/lib/python3.9/spyder/plugins/variableexplorer/widgets/collectionseditor.py", line 1708, in lessThan
    return leftData < rightData
TypeError: '<' not supported between instances of 'int' and 'str'
Traceback (most recent call last):
  File "/Applications/Spyder.app/Contents/Resources/lib/python3.9/spyder/plugins/variableexplorer/widgets/collectionseditor.py", line 1708, in lessThan
    return leftData < rightData
TypeError: '<' not supported between instances of 'str' and 'int'
Traceback (most recent call last):
  File "/Applications/Spyder.app/Contents/Resources/lib/python3.9/spyder/plugins/variableexplorer/widgets/collectionseditor.py", line 1708, in lessThan
    return leftData < rightData
TypeError: '<' not supported between instances of 'int' and 'str'
Traceback (most recent call last):
  File "/Applications/Spyder.app/Contents/Resources/lib/python3.9/spyder/plugins/variableexplorer/widgets/collectionseditor.py", line 1708, in lessThan
    return leftData < rightData
TypeError: '<' not supported between instances of 'int' and 'str'
Traceback (most recent call last):
  File "/Applications/Spyder.app/Contents/Resources/lib/python3.9/spyder/plugins/variableexplorer/widgets/collectionseditor.py", line 1708, in lessThan
    return leftData < rightData
TypeError: '<' not supported between instances of 'int' and 'str'
```

In variable explorer when sorting by `Size` leads to this issue reporter. 

In RStudio size represents memory footprint of the object than dimensions as present in Spyder. So RStudio sorting make sense. For spyder showing `shape` of different objects such sorting is bit weird. Populating memory footprint in `Size` column will be better.


Discussion:
Hey @AbhishekVermaDH, thanks for reporting. Please post a simple dataframe that generates this error so we can try to debug and fix it in our side.

Also, what's your Spyder version?
I have Spyder 4.2.1 installed on Big Sur. Everytime I click on an entry in the Variable Explorer pane, the whole Spyder window resize to a small size? What is the reason for this and how to fix it?
Replication steps:
1. Use code
```
import pandas as pd
import numpy as np
import datetime
df = pd.DataFrame({
    'A' : ['spam', 'eggs', 'spam', 'eggs'] * 6,
    'B' : ['alpha', 'beta', 'gamma'] * 8,
    'C' : [np.random.choice(pd.date_range(datetime.datetime(2013,1,1),datetime.datetime(2013,1,3))) for i in range(24)],
    'D' : np.random.randn(24),
    'E' : np.random.randint(2,10,24),
    'F' : [np.random.choice(['rand_1', 'rand_2', 'rand_4', 'rand_6']) for i in range(24)],
})
lst = list(range(10))
```
2. So there will be 2 variables created in the `Variable explorer`. Try sorting them by `Size` by clicking on text.  This will lead to following error screen with stacktrace:
```
  File "/Applications/Spyder.app/Contents/Resources/lib/python3.9/spyder/plugins/variableexplorer/widgets/collectionseditor.py", line 1708, in lessThan
    return leftData < rightData
TypeError: '<' not supported between instances of 'int' and 'str'
```
I am using Spyder 4.2.1 in MacOS Catalina. Also relevant:
```
Python 3.8.5 (default, Sep  4 2020, 02:22:02)
IPython 7.19.0 -- An enhanced Interactive Python.
``` 

Sorting by size does seem to work for me. But clicking on any variable in variable Explorer, the window resize automatically to smaller size.
Thanks @AbhishekVermaDH for the reproducible example. I was able to reproduce this bug with it.

We'll try to fix it for our next version (4.2.2), to be released in February 15th.

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
@@ -662,8 +662,8 @@ def setup_menu(self, minmax):
         self.empty_ws_menu = QMenu(self)
         add_actions(self.empty_ws_menu,
                     [self.insert_action_above, self.insert_action_below,
-                    self.insert_action, self.paste_action, None, 
-                    resize_action, resize_columns_action])
+                     self.insert_action, self.paste_action, None,
+                     resize_action, resize_columns_action])
         return menu
 
     # ------ Remote/local API -------------------------------------------------
@@ -1702,10 +1702,17 @@ def lessThan(self, left, right):
         """
         leftData = self.sourceModel().data(left)
         rightData = self.sourceModel().data(right)
-        if isinstance(leftData, str) and isinstance(rightData, str):
-            return natsort(leftData) < natsort(rightData)
-        else:
-            return leftData < rightData
+        try:
+            if isinstance(leftData, str) and isinstance(rightData, str):
+                return natsort(leftData) < natsort(rightData)
+            else:
+                return leftData < rightData
+        except TypeError:
+            # This is needed so all the elements that cannot be compared such
+            # as dataframes and numpy arrays are grouped together in the
+            # variable explorer. For more info see spyder-ide/spyder#14527
+            return True
+
 
 # =============================================================================
 # Tests


Test patch: