Sort by the number of items of each

Jeanne A. E. DeVoto revolution at jaedworks.com
Mon Dec 25 00:13:20 EST 2006


At 7:10 AM +0100 12/22/2006, Ludovic Thébault wrote:
>In the documentation, i saw this :
>"To create a custom sort order, use the each keyword to pass each line
>or item to a custom function. The value returned by the function is
>used as the sort key for that line or item."
>
>but i don't know how do this.


Mark is of course right that you can do a sort by the number of items in a line as a one-liner.

But in answer to your second question: There used to be a cookbook recipe, illustrating how to use custom sort orders, linked to the sort item in the dictionary... but I'm not sure where it's migrated to now. Here's the example:

--------
Solution:
function withoutLeadingArticle theTitle
  get word 1 of theTitle
  if it is "The" or it is "An" or it is "A" then
    -- we don't want to use it for the sort:
    delete word 1 of theTitle
  end if
  return theTitle
end withoutLeadingArticle

Discussion:
This custom function is called with a statement such as the following:

  sort lines of field "Titles" by withoutLeadingArticle(each)

The custom function works in tandem with the sort container command. When you sort the lines of a container, the each keyword serves as a placeholder for each line in the container. In the above statement, each line is passed to the "withoutLeadingArticle" function, and the returned value is used as the sort key for that line. The "withoutLeadingArticle" function simply removes any leading articles (the words "A", "An", and "The"), leaving the rest of the title to be used as the sort key.

-------
It's one of the most powerful of the lesser-known features of Transcript, that ability to do a custom sort order. You can sort cards this way as well.
-- 
jeanne a. e. devoto ~ revolution at jaedworks.com
http://www.jaedworks.com



More information about the use-livecode mailing list