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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T22:24:34+00:00 2026-05-11T22:24:34+00:00

Ok, I’ve got a list like this (just a sample of data): data =

  • 0

Ok, I’ve got a list like this (just a sample of data):

data = {"NAME": "James", "RANK": "3.0", "NUM": "27.5" ... }

Now, if I run something like this:

sortby = "NAME" //this gets passed to the function, hence why I am using a variable sortby instead
data.sort(key=itemgetter(sortby))

I get all the strings sorted properly – alphabetically.

However, when “sortby” is any of the floating values (RANK or NUM or any other), sort is done again, alphabetically, instead of numerically, so my sorted list looks something like this then:

0.441
101.404
107.558
107.558
108.48
108.945
11.195
12.143
12.801
131.73

which is obviously wrong.

Now, how can I do a sort like that (most efficiently in terms of speed and resources/computations taken) but have it cast the value somehow to float when it’s a float, and leave it as a string when it’s a string… possible? And no, removing quotes from float values in the list is not an option – I don’t have control over the source list, unfortunately (and I know, that would’ve been an easy solution).

  • 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-05-11T22:24:35+00:00Added an answer on May 11, 2026 at 10:24 pm

    If you want a general function which you can pass as a parameter to sort(key=XXX), then here’s a candidate complete with test:

    DATA = [
        { 'name' : 'A', 'value' : '10.0' },
        { 'name' : 'B', 'value' : '2.0' },
    ]
    
    def get_attr(name):
        def inner_func(o):
            try:
                rv = float(o[name])
            except ValueError:
                rv = o[name]
            return rv
        return inner_func
    
    for attrname in ('name', 'value'):
        DATA.sort(key=get_attr(attrname))
        print "%r-sorted: %s" % (attrname, DATA)
    

    When you run the above script, you get:

    'name'-sorted: [{'name': 'A', 'value': '10.0'}, {'name': 'B', 'value': '2.0'}]
    'value'-sorted: [{'name': 'B', 'value': '2.0'}, {'name': 'A', 'value': '10.0'}]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 121k
  • Answers 121k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I would suggest using a multi-column field as your base… May 12, 2026 at 12:34 am
  • Editorial Team
    Editorial Team added an answer If you're spelling it HTTP_REFERER (this is unclear due to… May 12, 2026 at 12:34 am
  • Editorial Team
    Editorial Team added an answer I'm assuming you mean a datetime picker in a winforms… May 12, 2026 at 12:34 am

Related Questions

OK, I know what you're thinking, "why write a method you do not want
Ok, I asked a question earlier about Flex and ADO.NET Data Services but didn't
Ok, I have been working with Solaris for a 10+ years, and have never
OK, I know there have already been questions about getting started with TDD ..
Ok, I have a strange exception thrown from my code that's been bothering me

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.