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

  • SEARCH
  • Home
  • 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 8603101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:14:41+00:00 2026-06-12T02:14:41+00:00

I tried to sort using sorted dir =[A1,A2,A10,A3] sorted(dir) My expected array is [A1,A2,A3,A10]

  • 0

I tried to sort using sorted

dir =["A1","A2","A10","A3"]
sorted(dir)

My expected array is

["A1","A2","A3","A10"]

but actual result is

["A1", "A10", "A2", "A3"]

How to sort array by name in python ?

  • 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-12T02:14:43+00:00Added an answer on June 12, 2026 at 2:14 am

    It is sorting by alphabetical order, so you need to break up the numbers and convert them to ints and sort with that. (Numbers in strings are treated as just characters, so it “sees” “A10” and tries to sort it first by “A”, then by “1”, and then by “0.”) For example:

    >>> sorted(dir, key=lambda x: int(x[1:]))
    ['A1', 'A2', 'A3', 'A10']
    

    If you are going to have other letters besides “A” in dir, you’ll need a more complicated sorting method, but it will be something along the same lines. (If you explain what dir contains more, I can write an example for that.) As mgilson’s comment points out, if the elements of dir follow the 1 char + number format, then you could take advantage of tuple sorting and do something like this:

    >>> dir.append('B12')
    >>> sorted(dir, key=lambda x: (x[0],int(x[1:])))
    ['A1', 'A2', 'A3', 'A10', 'B12']
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How to sort string names in array on ascending order . I tried sort
I tried to sort these number with Unix sort, but it doesn't seem to
I tried to search and found this: Sort an array by a child array's
I have a list that I need sorted by two fields. I've tried using
I am using this usort function to sort the array by value: function cmp($a,
In newer Python, I am able to use the sorted function and easily sort
I am using Python 3.2 and would like to sort a list of tuples
I tried sorting strings using bubblesort, but I dont know whether it makes any
I have a data with the following format: foo<tab>1.00<space>1.33<space>2.00<tab>3 Now I tried to sort
tried uncommenting pam_limits.so from the pam.d directory but no luck. Basic PAM seems to

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.