0

Hello,
I am not getting this sorting in python 3 with a key.

If I don't use the key I get:

sorted_ent_list = entry_list.sort()
TypeError: unorderable types: entry_T() < entry_T()

If I do use a member function to return private variable from entry_T class, then it says

sorted_ent_list = entry_list.sort(key=entry_T.get_word())
TypeError: get_word() takes exactly 1 positional argument (0 given)

but the only argument get_word takes is self....

I am missing something. I just want to sort the classes based on a private member variable accessed by get_word()

Someone help?

3
Contributors
2
Replies
4
Views
8 Years
Discussion Span
Last Post by Gribouillis
0

What is in entry_list, class instances? The simple solution is to create a list of lists with each sub-list containing [field_to_sort, class_instance] and just use the built in sort method.

sorted_ent_list = entry_list.sort(key=entry_T.get_word())

If entry_list is a list of class instances, then you would have to use something like
sorted_ent_list = [ entry_list[x].get_word(), entry_list[x] for x in range(0, len(entry_list))]
sorted_ent_list.sort()
but that is untested and highly suspect.

0

I think you should'nt call the method get_word. You should write

sorted_ent_list = entry_list.sort(key=entry_T.get_word)

sort needs a reference to the function.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.