5

I would like to know how can I sort a string by the number inside.

As example I have:

hello = " hola %d" % (number_from_database)
bye = "adios %d" % (number_from_database_again)

I want to sort them by the number even if it changes.

Martijn Pieters
  • 963,270
  • 265
  • 3,804
  • 3,187
Victor Castillo Torres
  • 10,061
  • 6
  • 37
  • 46
  • 1
    Do you have a list of these to sort? Sort them compared to what? – Martijn Pieters Jun 05 '13 at 17:42
  • for example sometimes the user can sorts in the web "bye" before than hello so hello = "hola 2" and bye = "adios 1" or sometimes the user can sorts hello before bye so i want to know how to sort them bye the number inside, thank you :D – Victor Castillo Torres Jun 05 '13 at 17:45
  • That doesn't make this any clearer. Sorting applies to a sequence of items; all you have is two variables, `hello` and `bye`. – Martijn Pieters Jun 05 '13 at 17:46
  • This is called "natural" sorting, see http://stackoverflow.com/q/11150239/989121 and the links therein for examples. – georg Jun 05 '13 at 17:49

3 Answers3

13

You can pass a key to sort:

sorted(l, key=lambda x: int(re.sub('\D', '', x)))

For example:

In [1]: import re

In [2]: l = ['asdas2', 'asdas1', 'asds3ssd']

In [3]: sorted(l, key=lambda x: int(re.sub('\D', '', x)))
Out[3]: ['asdas1', 'asdas2', 'asds3ssd']

Where re.sub('\D', '', x) replaces everything but the digits.

Community
  • 1
  • 1
Andy Hayden
  • 328,850
  • 93
  • 598
  • 514
1

Just a little complement to Andy's answer.

If you want to sort set which also contain strings without any number:

sorted(l, key=lambda x: int('0'+re.sub('\D', '', x)))

, which would put those strings without any number at the very beginning.

Yuan Tao
  • 437
  • 5
  • 7
-1
salutations = [hello, bye]
salutations.sort(key=lambda x: int(filter(lambda s: s.isdigit(), x.split())[0]))
mVChr
  • 48,301
  • 11
  • 105
  • 100