Sorting a word

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
giansuana
Posts: 16
Joined: Sat Mar 19, 2011 7:11 am

Sorting a word

Post by giansuana » Sun Mar 17, 2013 4:33 pm

I'd like to sort the characters of a word in ascending manner. Thinking of a word as a chunk, I tried something like that (while tWord holding a word):

Code: Select all

sort chars of tWord ascending text
But I get this error: compilation error at line 26 (Chunk: missing chunk) near "chars", char 4.
How can I accomplish my target?
Best, Gian-Reto.

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: Sorting a word

Post by sturgis » Sun Mar 17, 2013 4:46 pm

Probably not the best method (there is probably a way this can be done with a custom sort function) but it should work ok.

Code: Select all

on mouseUp
put "antidisestablishmentarianism" into tWord -- could turn this into a function instead taking a word as a parameter
repeat for each char tChar in tWord -- break the word into items of 1 char
put tChar & comma after tNewWord
end repeat
delete the last char of tNewWord -- remove trailing comma
sort items of tNewWord ascending -- sort by items
replace comma with empty in tNewWord -- nuke the commas
put tNewWord -- if turned into a function this would be "return tNewWord
end mouseUp

giansuana
Posts: 16
Joined: Sat Mar 19, 2011 7:11 am

Re: Sorting a word

Post by giansuana » Sun Mar 17, 2013 4:54 pm

Thank you for your answer. Works great as a function! :)

Post Reply