Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4594108
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T22:46:53+00:00 2026-05-21T22:46:53+00:00

I have a list of strings containing numbers and I cannot find a good

  • 0

I have a list of strings containing numbers and I cannot find a good way to sort them.
For example I get something like this:

something1
something12
something17
something2
something25
something29

with the sort() method.

I know that I probably need to extract the numbers somehow and then sort the list but I have no idea how to do it in the most simple way.

  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T22:46:54+00:00Added an answer on May 21, 2026 at 10:46 pm

    Perhaps you are looking for human sorting (also known as natural sorting):

    import re
    
    def atoi(text):
        return int(text) if text.isdigit() else text
    
    def natural_keys(text):
        '''
        alist.sort(key=natural_keys) sorts in human order
        http://nedbatchelder.com/blog/200712/human_sorting.html
        (See Toothy's implementation in the comments)
        '''
        return [ atoi(c) for c in re.split(r'(\d+)', text) ]
    
    alist=[
        "something1",
        "something12",
        "something17",
        "something2",
        "something25",
        "something29"]
    
    alist.sort(key=natural_keys)
    print(alist)
    

    yields

    ['something1', 'something2', 'something12', 'something17', 'something25', 'something29']
    

    PS. I’ve changed my answer to use Toothy’s implementation of natural sorting (posted in the comments here) since it is significantly faster than my original answer.


    If you wish to sort text with floats, then you’ll need to change the regex from one that matches ints (i.e. (\d+)) to a regex that matches floats:

    import re
    
    def atof(text):
        try:
            retval = float(text)
        except ValueError:
            retval = text
        return retval
    
    def natural_keys(text):
        '''
        alist.sort(key=natural_keys) sorts in human order
        http://nedbatchelder.com/blog/200712/human_sorting.html
        (See Toothy's implementation in the comments)
        float regex comes from https://stackoverflow.com/a/12643073/190597
        '''
        return [ atof(c) for c in re.split(r'[+-]?([0-9]+(?:[.][0-9]*)?|[.][0-9]+)', text) ]
    
    alist=[
        "something1",
        "something2",
        "something1.0",
        "something1.25",
        "something1.105"]
    
    alist.sort(key=natural_keys)
    print(alist)
    

    yields

    ['something1', 'something1.0', 'something1.105', 'something1.25', 'something2']
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have a list containing strings that look like this: 00x000s00g00wfds0d dkdf00sdf00sdfg00jk kf00dfjkd0sdl0sd0f0
I have a list containing version strings, such as things: versions_list = [1.1.2, 1.0.0,
I have an Excel spreadsheet containing a list of strings. Each string is made
I have list of strings like this FirstName-Lastname (separated by a dash -) I
I have a List of strings (GUIDS) and I would like to take one
I have a list of strings containing about 7 million items in a text
If I have a std::string containing a comma-separated list of numbers, what's the simplest
I have a list of lists, each containing a different number of strings. I'd
Can I have a List containing one string and two numbers? Or I can
I have string containing a list of name like below: John asked Kim, Kelly,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.