This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: [3.2] sorted(big dict)
Type: behavior Stage:
Components: Versions: Python 3.2
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: DDarko, mark.dickinson
Priority: normal Keywords:

Created on 2011-06-13 10:18 by DDarko, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
sort_test.py DDarko, 2011-06-13 10:18 example, to reproduce the bug
Messages (6)
msg138231 - (view) Author: DDarko (DDarko) Date: 2011-06-13 10:18
I added an example to reproduce the bug.
From the command line the same code:
Python 2.7.1+ (r271:86832, Apr 11 2011, 18:05:24) [GCC 4.5.2] on linux2

$ python sort_test.py

Everything fine.

Python 3.2 (r32:88445, Mar 25 2011, 19:28:28) [GCC 4.5.2] on linux2

$ python3 sort_test.py 
Traceback (most recent call last):
  File "sort_test.py", line 1821, in <module>
    r = sorted(d.items(), key=operator.itemgetter(1), reverse=1)
TypeError: unorderable types: dict() < dict()
msg138232 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2011-06-13 11:33
This is expected behaviour:  Python 3 changed the semantics of the comparison operators <, <=, >, >=.  See:

http://docs.python.org/py3k/whatsnew/3.0.html#ordering-comparisons

for more.
msg138233 - (view) Author: DDarko (DDarko) Date: 2011-06-13 12:12
I am aware of this change.
In this example, I'm sort by item number 1, which is a list, and its first value is an int.

$ python3 sort_test.py 
<class 'list'>
<class 'list'>
<class 'list'>
<class 'list'>
<class 'list'>
<class 'list'>
...

Dict index is always No. 2. But I do not sort it.
That's why it surprised me this error because nowhere dicts should not be compared.
msg138237 - (view) Author: DDarko (DDarko) Date: 2011-06-13 12:36
Sure. I know what's going on.
Sorry for the inconvenience.
msg138238 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2011-06-13 12:42
> In this example, I'm sort by item number 1, which is a list, and its
> first value is an int.

?  You're sorting by the values of the dict d, and those values have the form [int, int, dict];  so when the two ints match (e.g., in your data, there are two values of the form [64, 124, {...}]) there's a dictionary comparison.

Did you mean to do:

    sorted(d.values(), key=operator.itemgetter(1), reverse=1)

?
msg138241 - (view) Author: DDarko (DDarko) Date: 2011-06-13 12:56
I am interested in sorting only by INT0, in this example:
{k: [INT0, INT1, DICT], k: [INT0, INT1, DICT], ...}
not cmp. whole lists.

Unfortunately I can not take advantage of .values() as the keys I need.
History
Date User Action Args
2022-04-11 14:57:18adminsetgithub: 56533
2011-06-13 12:56:52DDarkosetmessages: + msg138241
2011-06-13 12:42:11mark.dickinsonsetmessages: + msg138238
2011-06-13 12:36:52DDarkosetmessages: + msg138237
2011-06-13 12:12:23DDarkosetmessages: + msg138233
2011-06-13 11:33:32mark.dickinsonsetstatus: open -> closed

nosy: + mark.dickinson
messages: + msg138232

resolution: not a bug
2011-06-13 10:18:25DDarkocreate