2

I am trying to sort a list of objects in Python 3.4 based on the value of the data attribute of each object. If I use:

db[count].sort(key=lambda a: a.data)

everything works fine. However, I want the sort to be case insensitive so I use

db[count].sort(key=lambda a: a.data.lower)

but then I get:

db[count].sort(key=lambda a: a.data.lower)

TypeError: unorderable types: builtin_function_or_method() < builtin_function_or_method()

Any ideas?

2
  • 3
    You need to call lower() from your lambda. Commented May 2, 2014 at 8:47
  • Sorry sussed it. I needed db[count].sort(key=lambda a: a.data.lower()) Commented May 2, 2014 at 8:50

2 Answers 2

4

key has to be a callable that returns a value to be sorted. In your case it returns another callable a.data.lower. You need to call lower in order to get the value, so the correct form is:

db[count].sort(key=lambda a: a.data.lower())
Sign up to request clarification or add additional context in comments.

Comments

4

You are passing a reference to the lower method instead of calling it.

Try this:

db[count].sort(key=lambda a: a.data.lower())

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.