Path: csiph.com!usenet.pasdenom.info!aioe.org!rt.uk.eu.org!feed.xsnews.nl!border-2.ams.xsnews.nl!xlned.com!feeder1.xlned.com!newsfeed.xs4all.nl!newsfeed6.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.009 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'desirable.': 0.07; 'int)': 0.09; "('',": 0.16; '(str,': 0.16; '(x,': 0.16; '-1))': 0.16; 'keys:': 0.16; 'oct': 0.16; 'sorts': 0.16; 'wed,': 0.16; 'string': 0.17; 'wrote:': 0.17; 'input': 0.18; 'sort': 0.21; '31,': 0.22; 'tuples': 0.22; 'example': 0.23; 'split': 0.23; 'header:In-Reply- To:1': 0.25; 'am,': 0.27; 'message-id:@mail.gmail.com': 0.27; 'case,': 0.29; 'could': 0.32; 'to:addr:python-list': 0.33; 'received:google.com': 0.34; 'sequence': 0.35; 'received:209.85': 0.35; 'really': 0.36; 'two': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'think': 0.40; 'construct': 0.84; 'to:name:python': 0.84; 'do:': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=qYgjZynEhx/jX1rXid90pt+8fmmax9szUDisLsSje5k=; b=SOr0su9qPBvnyH3pzdbD8Gj79/4qgqgKfGL6adryQMEkWU8RMivrsJM3+4T/t6saQn +zS3CUyyg2ne/To0Dm8R0reYwBQf7DV3aXP2HFPT+iJcM6iKNHWdoyc0YnmioaSMej01 dAfgLG6XK9ZMzR825j2rOQ23Qe67uXhDrm8MKRo3Z8pToj6kIxl+GnPMwFSwoPnRzixL opkevX2GKz/uI6QvikF9TIa9Umdkx94ZAyXz4jnEh7EAqsw/zpx4aDs3JQVztwyzEdSV FU9ahZpttqMIPfeM+vJmlAoIpJtS9hdZYchE2KrpajRdaUl+pjEUXi98ETcq465CClhP zf6Q== MIME-Version: 1.0 In-Reply-To: References: From: Ian Kelly Date: Wed, 31 Oct 2012 09:44:10 -0600 Subject: Re: sort order for strings of digits To: Python Content-Type: text/plain; charset=ISO-8859-1 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: 16 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1351698283 news.xs4all.nl 6889 [2001:888:2000:d::a6]:38142 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:32526 On Wed, Oct 31, 2012 at 9:17 AM, djc wrote: > The best I can think of is to split the input sequence into two lists, sort > each and then join them. In the example you have given they already seem to be split, so you could just do: sorted(n, key=int) + sorted(s) If that's not really the case, then you could construct (str, int) tuples as sort keys: sorted(n+s, key=lambda x: ('', int(x)) if x.isdigit() else (x, -1)) Note that the empty string sorts before all numbers here, which may or may not be desirable.