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?

Recommended Answers

All 2 Replies

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.