954,541 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Sort in opposite direction

How do I have the algorithm below to sort my list in opposite direction (right to left) instead of left to right.
Also, how do I make it sort simultaneously or alternatively in both directions? Thanks.

def bubblesort(l):
    for passes in range(len(l)-1, 0, -1):
        for index in range(passes):
            if l[index] < l[index + 1]:
                l[index], l[index + 1] = l[index + 1], l[index]
                print l
    return l

bubblesort([4,2,7,9,25])

Editor:
Please use code tags with your code to preserve the indentations. Otherwise the code is very difficult to read and not too many folks will help.[code=python]
your Python code here
[/code]

nonang
Newbie Poster
3 posts since Apr 2009
Reputation Points: 10
Solved Threads: 0
 

In your code line:
if l[index] < l[index + 1]:
use:
if l[index] > l[index + 1]:
for ascending sort order.

BTW, programmers avoid using the letter l for a variable name, since it looks so much like 1.

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You