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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T22:34:39+00:00 2026-06-08T22:34:39+00:00

Okay, so I have a bit of a problem, Im new to python sorry.

  • 0

Okay, so I have a bit of a problem, Im new to python sorry.

I am trying to sort a list by score, which is a number, but if there is a draw I need to sort them by the length of their name, the shorter the name the higher the rank.

So I have this list here

11 Jenny
8 Adam
10 Mark
8 Ada

and when I use this code here on it, it just comes back with

11 Jenny
10 Mark
10 Mark
10 Mark

def sort(names, counts):
    newArr = []
    newNames = names
    newCount = counts
    for x in range(0, len(names)):
        newArr.append(findBiggest(newNames, newCount))
        for z in range(0, len(names)):
            name = newArr[len(newArr) - 1].split(" ")[1]
            print name 
            if names[z] == name:
                tempArr1 = newNames
                tempArr2 = newCount
                newNames = []
                newCount = []
                for y in range(0, len(tempArr1)):
                    if y != z:
                        newNames.append(tempArr1[y])
                        newCount.append(tempArr2[y])
    return newArr
def findBiggest(names, counts):
    biggest = 0;
    for x in range(0, len(counts)):
        if int(counts[x]) > biggest:
            biggest = int(counts[x])
    biggestCountArr = [[], []]
    for x in range(0, len(counts)):
        if int(counts[x]) == biggest:
            biggestCountArr[0].append(counts[x])
            biggestCountArr[1].append(names[x])
    if len(biggestCountArr[0]) == 1:
        return str(biggestCountArr[0][0]) + " " + biggestCountArr[1][0]
    else:
        return smallestLength(biggestCountArr)
def smallestLength(twoDArr):
    names = twoDArr[1]
    shortestLen = 0
    for x in range(0, len(names)):
        if len(names[x]) > shortestLen:
            shortestlen = len(names[x])
    for x in range(0, len(names)):
        if len(names[x]) == shortestLen:
            return str(twoDArr[0][x]) + " " + twoDArr[1][x]

Just so you know

11 Jenny
8 Adam
10 Mark
8 Ada

should come out as

11 Jenny
10 Mark
8 Ada
8 Adam
  • 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-08T22:34:42+00:00Added an answer on June 8, 2026 at 10:34 pm
    lst=[(11, "Jenny"),(8, "Adam"),(10, "Mark"),(8, "Ada")]
    lst.sort(key=lambda x: (-x[0],len(x[1])) )
    print (lst) # [(11, 'Jenny'), (10, 'Mark'), (8, 'Ada'), (8, 'Adam')]
    

    The list method sort and the builtin function sorted accept a keyword argument key which is given a callable. Basically, for every element in the sequence, that element is passed to the key function and the return value of that function is actually what python uses to determine ordering when sorting. So, in the above, I use lambda to construct a function which returns a tuple from the input elements. The tuple is ordered first_element, lenth_of_second_element.

    When tuples (or lists for that matter) are compared, it’s much like comparing a string. You look at the first element, if they’re the same, you continue on to look at the second element, then the third and so on until one element is greater than the other. e.g.

    (1,2,3,4) > (1,2,3,3) #True
    

    This ends up being handy for sorting in very interesting ways.

    I suppose to round this out, I should mention that the algorithm that python uses to sort is stable. This means that if you sort by keyA and then you sort by keyB, two elements which compare equal based on keyB will maintain the order they had after the sort using keyA. In other words, a sort doesn’t change the order of equal valued elements. So, the above could also be accomplished like this:

    lst.sort(key=lambda x:len(x[1]))  #sort by length of names
    lst.sort(key=lambda x:x[0], reversed=True) #sort by score (highest first instead of regular lowest first)
    

    And I suppose no answer is complete without a link to something which explains it more elegantly. (Specifically, see the section on “Key Functions”)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay I have updated my code quite a bit. I am getting a new
Okay, I have done a bit of searching online and found this thread, but
Okay, here is the problem. I have an unordered list with a bunch of
Okay, this problem is a bit odd. I have an application built in VS2010
I have a bit of problem with this. I have a class A which
Okay, so this is a bit confusing (well to me). I have a string
okay i have been trying to understand this for hours i am learning VB
Okay I have a series of objects based on a base class which are
Okay I have updated my code a little, but I am still not exactly
Okay, this is a bit messy: I'm using Netbeans, and I have a main

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.