-5

Possible Duplicate:
In Python, how can I naturally sort a list of alphanumeric strings such that alpha characters sort ahead of numeric characters?

How do you sort a string containing numbers and characters in python?

Community
  • 1
  • 1
  • http://www.whathaveyoutried.com – Gareth Latty Oct 15 '12 at 15:19
  • 2
    @user1742700: after a question of yours is closed, please don't ask a very similar question a few minutes later. Ask why the question was closed -- in this case, no effort shown and no discussion of obvious questions (should 2 come before or after 17? what about "A2" vs "A17"? does case matter) -- and try to write a better one. – DSM Oct 15 '12 at 15:26

1 Answers1

3
>>> s = '13abc3'
>>> ''.join(sorted(s, key=lambda x: int(x) if x.isdigit() else x))
'133abc'

This doesn't handle any custom sorting if that is what you're after (this goes number, uppercase, lowercase).

RocketDonkey
  • 36,383
  • 7
  • 80
  • 84