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 8021293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:56:11+00:00 2026-06-04T21:56:11+00:00

I have the next list: a = [‘1th Word’, ‘Another Word’, ’10th Word’] print

  • 0

I have the next list:

a = ['1th Word', 'Another Word', '10th Word']
print a.sort()
>>> ['10th Word', '1th Word', 'Another Word']

But I need:

['1th Word', '10th Word','Another Word']

Is there an easy way to do this?

I tried:

r = re.compile(r'(\d+)')
def sort_by_number(s):
    m = r.match(s)
    return m.group(0)

x.sort(key=sort_by_number)

But some strings do not have numbers and this leads to an errors.
Thanks.

  • 1 1 Answer
  • 0 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-06-04T21:56:14+00:00Added an answer on June 4, 2026 at 9:56 pm

    Here’s a function that works for the general case

    import re
    def natkey(s):
        return [int(p) if p else q for p, q in re.findall(r'(\d+)|(\D+)', s)]
    
    x = ['1th Word', 'Another Word 2x', 'Another Word 20x', '10th Word 10', '2nd Word']
    
    print sorted(x)
    print sorted(x, key=natkey)
    

    Result:

    ['10th Word 10', '1th Word', '2nd Word', 'Another Word 20x', 'Another Word 2x']
    ['1th Word', '2nd Word', '10th Word 10', 'Another Word 2x', 'Another Word 20x']
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have the next list: SortedList<Decimal, KeyValuePair<string,string>> datos; And i need display this in
I have the next C code: #include <stdio.h> #include <stdlib.h> #include list.h #include graph.h
i have written the next code: public void delete(MyType instance) { List<MyType> myList =
I have this list of people with checkboxes next to their names. When you
I have a form with a list of people with check boxes next to
I have the following problem: list.c struct nmlist_element_s { void *data; struct nmlist_element_s *next;
Say I have a list of items from a database. Next to each item
I have a function, that returns the next higher value of a Dictionary-Keys-List compared
Greetings, I have a list of user comments on a page, with a next
I have the next object/list ListaDatos and I like to get it as clear

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.