0

I have a class that's sole purpose is to sort an array that's given as a parameter, however first of all, I'm getting an error "int not iterable." My question is what is this error and am I doing my sorting correctly? Thanks. Here is my sorter class being called by the other class below.

class Sorter:
    def reverseWackySort(self, vals):
        i = 0
        j = 0
        temp = 0
        length = len(vals)
        for i in length:
            for j in (length -1):
                if vals[j] > vals[j+1]:
                    temp = vals[j]
                    vals[j] = vals[j+1]
                    vals[j+1] = temp;
        return vals

Code -

from Sorter import Sorter

def TestSorter():
    rws = Sorter()


    nums = [88, 1, 7, 32, 18, 77, 34, 99, 54, 22]

    print "\nBefore Sort: ", nums

    rws.reverseWackySort(nums)

    print "After Sort:   {}\n".format(nums)

TestSorter()
4
  • 1
    Why are you using a class in the first place? What is a Sorter instance for? This isn't Java, where you have to make every function a method… More importantly, do you have any reason to believe the class is relevant to the problem? If not, given that explaining it takes up half your text, and the implementation causes an IndentationError, and it draws more attention than your actual problem… maybe it would be better to trim things down to an SSCCE. Commented Jul 2, 2013 at 21:54
  • Is there a particularly good reason why you aren't using sorted or list.sort? Commented Jul 2, 2013 at 21:54
  • 1
    It's homework actually, practicing not using built-in methods :) Commented Jul 2, 2013 at 21:55
  • 1
    @Binka: That is a perfectly acceptable reason! Good luck in your studies. Commented Jul 2, 2013 at 21:56

1 Answer 1

8

You are trying to iterate over an integer:

for i in length:
    for j in (length -1):

Perhaps you wanted to iterate over the range instead:

for i in range(length):
    for j in range(length -1):

range() produces a sequence of integers up to (but not including) length for you.

Note that you can swap variables in place in Python without a temporary intermediary:

vals[j], vals[j+1] = vals[j+1], vals[j]

and you don't need to 'pre-declare' variables either; the i = 0, j = 0 and temp = 0 lines are redundant.

Sign up to request clarification or add additional context in comments.

6 Comments

Thank you. That was my problem :)
Done, I have a quick question if you don't mind. I fixed it to sort by decreasing order instead. Now that my list is sorted. I have to try and sort it in this pattern. "largest -> smallest -> next largest -> next smallest ->" etc. Any ideas? For example, in the other direction in Java, I was able to do this
@Binka: You should open a new question for that.
@Binka: It's impossible to format code in questions. In this case, as John Y says, you should open a new question. When that isn't appropriate, sometimes editing your existing question is. When neither is appropriate, just post it somewhere like pastebin.com, and put a link here.
@Binka: But one quick hint: The easiest way to do it is to figure out how to build that structure out of an already-sorted list. Then you can just use your existing sort function, then that new function.
|

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.