Sorting A List of Words by Length of Characters

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
deeverd
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 165
Joined: Mon Jul 16, 2007 11:58 pm

Sorting A List of Words by Length of Characters

Post by deeverd » Sat Dec 24, 2011 5:54 pm

Hello Anyone,

I've been struggling to find a simple way to use the sort command to sort a list of words by the size (character length) of each word. For example, I want all the words with the same amount of letters to be grouped together in descending order. Before asking this question, I spent some hours playing with different lines of script, reading up on the sort command, etc. The following line is the closest I've come to making it work:

sort lines of field "textField" descending by the length of each

That line "mostly" works, but some of the groups of words with the same number of characters are out of order (not exactly descending). For instance, words that have 10 or more letters in them are strangely placed in between the group of words that have 2 characters and 1 character.

Any suggestions?

Thanks so much in advance.

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2729
Joined: Sat Dec 22, 2007 5:35 pm
Contact:

Re: Sorting A List of Words by Length of Characters

Post by jmburnod » Sat Dec 24, 2011 6:50 pm

Hi Devert,

Code: Select all

on SortByLength 
   put empty into rSortByLength
   put fld "myFld" into tText
   repeat for each word tLine in tText
      put  tLine&"," &the length of tLine&return after rSortByLength
   end repeat
   delete char -1 of rSortByLength
   sort lines of rSortByLength descending numeric by item 2 of each
   put rSortByLength
end SortByLength
Best regards

Jean-Marc
https://alternatic.ch

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

Re: Sorting A List of Words by Length of Characters

Post by sturgis » Sat Dec 24, 2011 7:39 pm

As JM said, since you're sorting by length of each you need to specify numeric. (think a discussion of the same was on the use-list too)

deeverd
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 165
Joined: Mon Jul 16, 2007 11:58 pm

Re: Sorting A List of Words by Length of Characters

Post by deeverd » Sat Dec 24, 2011 8:48 pm

Thank you JM and Sturgis,

Your code worked like a charm, although I modified it slightly to create a list of words by length that were also in alphabetical order. For the benefit of anyone else who may eventually need to do the same thing, here's my version of the script below:

Code: Select all

on mouseUp
   put empty into rSortByLength
   put field "myField" into tText
   repeat for each word tLine in tText
      put  tLine&"," &the length of tLine&return after rSortByLength
   end repeat
   delete char -1 of rSortByLength
   sort lines of rSortByLength ascending-- sort text alphabetically a to z
   sort lines of rSortByLength descending numeric by item 2 of each
   put rSortByLength into field "wordLengthField"
end mouseUp
Anyway, you definitely answered my question perfectly, which made for a very nice Christmas present today.

Merry Christmas to all of you.

Cheers,
deeverd

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10309
Joined: Wed May 06, 2009 2:28 pm

Re: Sorting A List of Words by Length of Characters

Post by dunbarx » Sat Dec 24, 2011 9:04 pm

I was the original poster on this. I wanted to sort by the "length" of each line, where a 12 char line would come after a 4 char line.

Say that yourList contained:

AAA
BBBBBB
CCCCCCCCCCC

You write: "sort yourList by the length of each"

Each line is evaluated to an integer, via the "length" function, so that LC resolves this list to:

3
6
11

Unless you sort numerically, the "11" will come before the others, since it starts with a "1". This is how it should, being as written a text sort. There is no function that looks at the length of a line a humans do, in terms of how wide it appears. Sorting numeric fixes the disconnect between people and machines.

Craig Newman

Post Reply