23

I have tried to figure this out in different ways, without any success. I keep getting an ascending order sort, rather than descending order when I print.

ListB = [24, 13, -15, -36, 8, 22, 48, 25, 46, -9]
sorted(ListB, key=int, reverse=True)
print sorted(ListB)
3
  • 1
    reversed(sorted(listb)) Commented Aug 19, 2014 at 0:57
  • 2
    You typed print sorted(ListB), which, quite naturally, prints ListB sorted. Commented Aug 19, 2014 at 0:58
  • Related: Sort a list of numerical strings in ascending order Commented Jul 31, 2023 at 21:41

7 Answers 7

23

You are printing the list sorted ascending:

print sorted(ListB)

If you want it descending, put your print statement on the previous line (where you reverse it)

print sorted(ListB, key=int, reverse=True)

Then remove your final print statement.

Example:

>>> ListB = [24, 13, -15, -36, 8, 22, 48, 25, 46, -9]
>>> print sorted(ListB, key=int, reverse=True)
[48, 46, 25, 24, 22, 13, 8, -9, -15, -36]
Sign up to request clarification or add additional context in comments.

Comments

15

Try this. It'll sort the list in-place in descending order (there isn't any need to specify a key in this case):

listB = [24, 13, -15, -36, 8, 22, 48, 25, 46, -9]
listB.sort(reverse=True) # listB gets modified

print listB
=> [48, 46, 25, 24, 22, 13, 8, -9, -15, -36]

Alternatively, you can create a new sorted list:

listB = [24, 13, -15, -36, 8, 22, 48, 25, 46, -9]
listC = sorted(listB, reverse=True) # listB remains untouched

print listC
=> [48, 46, 25, 24, 22, 13, 8, -9, -15, -36]

1 Comment

Your suggestion is a good one since I will eventually need a new list. I am teaching myself Python by manually creating a box and whisker plot. Sorting the list is important. Thanks again!
2
ListB = [24, 13, -15, -36, 8, 22, 48, 25, 46, -9]

ListB = sorted(ListB, key=int, reverse=True)

print ListB

Sorted does not change the variable passed to it. So if you want to do anything with them you have to store sorted output into a variable.

Comments

0
reversed(sorted(listb))

This creates an iterable going from 48 -> -36

1 Comment

just reversed(ListB).
0

You should have combined these two lines of code together, using this instead.

print sorted(ListB, key=int, reverse=True)

Result:

[48, 46, 25, 24, 22, 13, 8, -9, -15, -36]

Comments

0

In Python you can sort as follows:

listA = [150, 120, 100, 165, 190, 145, 182, 175, 17]
print(sorted(listA))
print(sorted(listA, reverse=True))

This would be the actual implementation using selection sort:

# Traverse through all array elements
for i in range(len(listA)):

    # Find the minimum element in remaining
    # unsorted array
    min_idx = i
    for j in range(i + 1, len(listA)):
        if listA[min_idx] > listA[j]:
            min_idx = j

    # Swap the found minimum element with
    # the first element
    listA[i], listA[min_idx] = listA[min_idx], listA[i]

print(listA)

# Traverse through all array elements
for i in range(len(listA)):

    # Find the minimum element in remaining
    # unsorted array
    min_idx = i
    for j in range(i + 1, len(listA)):
        if listA[min_idx] < listA[j]:
            min_idx = j

    # Swap the found minimum element with
    # the first element
    listA[i], listA[min_idx] = listA[min_idx], listA[i]

print(listA)

Comments

-2
ListB = [24, 13, -15, -36, 8, 22, 48, 25, 46, -9]
ListB.sort()
print(ListB[::-1])

This should work

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.