Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #32526

Re: sort order for strings of digits

References <k6rfdu$rgn$1@news.albasani.net>
From Ian Kelly <ian.g.kelly@gmail.com>
Date 2012-10-31 09:44 -0600
Subject Re: sort order for strings of digits
Newsgroups comp.lang.python
Message-ID <mailman.3121.1351698283.27098.python-list@python.org> (permalink)

Show all headers | View raw


On Wed, Oct 31, 2012 at 9:17 AM, djc <djc@kangoo.invalid> 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.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

sort order for strings of digits djc <djc@kangoo.invalid> - 2012-10-31 15:17 +0000
  Re: sort order for strings of digits Hans Mulder <hansmu@xs4all.nl> - 2012-10-31 16:31 +0100
  Re: sort order for strings of digits Ian Kelly <ian.g.kelly@gmail.com> - 2012-10-31 09:44 -0600
  Re: sort order for strings of digits Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-10-31 14:17 -0400
  Re: sort order for strings of digits Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-10-31 21:33 +0000
  Re: sort order for strings of digits Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2012-10-31 19:05 -0400
    Re: sort order for strings of digits Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-10-31 23:44 +0000
      Re: sort order for strings of digits Chris Angelico <rosuav@gmail.com> - 2012-11-01 11:53 +1100
        Re: sort order for strings of digits Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-11-02 00:27 +0000
  Re: sort order for strings of digits Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-10-31 23:09 +0000
    Re: sort order for strings of digits DJC <djc@news.invalid> - 2012-10-31 23:45 +0000
    Re: sort order for strings of digits Arnaud Delobelle <arnodel@gmail.com> - 2012-11-01 00:59 +0000
  Re: sort order for strings of digits Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-11-01 00:30 +0000
  Re: sort order for strings of digits wxjmfauth@gmail.com - 2012-11-01 01:52 -0700

csiph-web