Hi,

I have a Text widget and a button that is supposed to sort the Text's contents alphabetically.


This time I won't ask you how to do it, I'll rather ask you to correct me.

This is how I solved the problem:

unsortedT = text1.get(1.0, END).replace("\n", "\n*").split("*")
sortedT = sorted(unsortedT)
text1.delete(1.0, END)
text1.insert(END, "".join(sortedT))

by the way, the user is always told to write the contents in the following format:

word1
word2
word3

How bad/unelegant is it?

Edited 6 Years Ago by G_S: n/a

Why don't you do, unsortedT = text1.get(1.0, END).split("\n") and then text1.insert(END, "\n".join(sortedT)

Ah!! But of course... Why didn't I see it...

Thanks :)

Any other suggestions???

Edited 6 Years Ago by G_S: n/a

list.sort()? Is that what you are looking for?

Um, sorry, I don't understand. Could you explain what your code does?

The idea here was converting a string (which is visually presented as a list) into a python list, sorting it alphabetically, and then turning it back into a string.

this was done so that the user would get a list that loooks like a list to humans:

Aaaaaaaa
Bbbbbbbbbbb
Cccccccccc


and not a python list:

wich doesn't look like a 'human' list.

You mean:

humanlist="\n".join(pythonlist)

??

pythonlist = ['Bbbbbbbbb','Aaaaaaaa',  'Ccccccccc' ]
humanlist="\n".join(sorted(pythonlist))
print humanlist
"""Output:
Aaaaaaaa
Bbbbbbbbb
Ccccccccc
"""
This question has already been answered. Start a new discussion instead.