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?

Recommended Answers

All 6 Replies

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???

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

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)

??

You mean:

humanlist="\n".join(pythonlist)

??

pythonlist = ['Bbbbbbbbb','Aaaaaaaa',  'Ccccccccc' ]
humanlist="\n".join(sorted(pythonlist))
print humanlist
"""Output:
Aaaaaaaa
Bbbbbbbbb
Ccccccccc
"""
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.