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.
Sorting A List of Words by Length of Characters
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
Re: Sorting A List of Words by Length of Characters
Hi Devert,
Best regards
Jean-Marc
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
Jean-Marc
https://alternatic.ch
Re: Sorting A List of Words by Length of Characters
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)
Re: Sorting A List of Words by Length of Characters
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:
Anyway, you definitely answered my question perfectly, which made for a very nice Christmas present today.
Merry Christmas to all of you.
Cheers,
deeverd
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
Merry Christmas to all of you.
Cheers,
deeverd
Re: Sorting A List of Words by Length of Characters
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
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