Path: csiph.com!usenet.pasdenom.info!news.etla.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed2a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; '16,': 0.03; 'python3': 0.07; 'difference,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'seemed': 0.09; 'types:': 0.09; 'python': 0.11; 'wrote': 0.14; '(2,': 0.16; '(3,': 0.16; '2.7.3': 0.16; 'igor': 0.16; 'python3.': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'skip:n 70': 0.16; 'typeerror:': 0.16; 'types,': 0.16; 'sat,': 0.16; 'wrote:': 0.18; 'bit': 0.19; 'feb': 0.22; '>>>': 0.22; 'example': 0.22; '15,': 0.26; 'header:X -Complaints-To:1': 0.27; 'appreciated.': 0.29; 'work.': 0.31; '"",': 0.31; 'keys': 0.31; 'file': 0.32; 'another': 0.32; '(most': 0.33; 'received:co.za': 0.34; 'received:za': 0.34; 'skip:d 20': 0.34; 'could': 0.34; 'but': 0.35; 'machine.': 0.36; 'to:addr :python-list': 0.38; 'pm,': 0.38; 'recent': 0.39; '12,': 0.39; 'to:addr:python.org': 0.39; 'changed': 0.39; 'received:org': 0.40; 'even': 0.60; 'day.': 0.63; 'skip:n 10': 0.64; 'more': 0.64; '30,': 0.65; 'here': 0.66; 'frank': 0.68; 'hints': 0.68; 'results': 0.69; 'received:41': 0.70; 'trick,': 0.84; 'from.': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: "Frank Millman" Subject: Re: Sorting dictionary by datetime value Date: Sat, 8 Feb 2014 10:03:42 +0200 References: X-Gmane-NNTP-Posting-Host: 41-135-99-195.dsl.mweb.co.za X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.3790.4657 X-RFC2646: Format=Flowed; Original X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.4913 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 49 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1391846633 news.xs4all.nl 2876 [2001:888:2000:d::a6]:38254 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:65639 "Chris Angelico" wrote in message news:CAPTjJmqDusdFC1eLbU6LF5-up__LAE-63ii0UUvAGGNem9U4+w@mail.gmail.com... > On Sat, Feb 8, 2014 at 6:06 PM, Igor Korot wrote: >>>>> sorted(a.items(), key=a.get) >> [('1', datetime.datetime(2012, 12, 28, 12, 15, 30, 100)), ('3', >> datetime.datetim >> e(2012, 12, 28, 12, 16, 44, 100)), ('2', datetime.datetime(2012, 12, 28, >> 12, 17, >> 29, 100))] That seemed like a neat trick, so I thought I would try to understand it a bit better in case I could use it some day. I am using python3. I don't know if that makes a difference, but I cannot get it to work. >>> d = {1: 'abc', 2: 'xyz', 3: 'pqr'} >>> sorted(d.items(), key=d.get) Traceback (most recent call last): File "", line 1, in TypeError: unorderable types: NoneType() < NoneType() >>> I know that python3 is stricter regarding ordering of non-comparable types, but I don't see where None is coming from. I have python 2.7.3 on another machine. Here are the results - >>> d = {1: 'abc', 2: 'xyz', 3: 'pqr'} >>> sorted(d.items(), key=d.get) [(1, 'abc'), (2, 'xyz'), (3, 'pqr')] It did not crash, but it did not sort. Then I changed the keys to strings, to match Igor's example - >>> d = {'1': 'abc', '2': 'xyz', '3': 'pqr'} >>> sorted(d.items(), key=d.get) [('1', 'abc'), ('3', 'pqr'), ('2', 'xyz')] It works - now I am even more confused. Any hints will be appreciated. Frank Millman