Page 2 of 3

Re: Sort question

Posted: Sun Apr 03, 2022 10:38 pm
by dunbarx
Richmond.

As I said, I am not smart enough to have made that up. It lives somewhere.

And I cannot imagine, though I will check tomorrow, that it might work in HC but not in LC.

Craig

Re: Sort question

Posted: Sun Apr 03, 2022 11:03 pm
by FourthWorld
I can recall seeing some sort of "and" clause for something like this, so if Craig's crazy he and I suffer from the same affliction. :)

Was it perhaps related to marked cards?

Re: Sort question

Posted: Sun Apr 03, 2022 11:14 pm
by FourthWorld
Found it: page 366 of "HyperTalk 2.2: The Book" describes the "mark" command as supporting multiple search options with "and".

The LC Dict has no such example, but attempting it throws no error (I haven't yet put together an example to see if it actually works).

If this does indeed work in LC for using multiple search criteria, it would be helpful to see that part of the parser extended for use with the "sort" command and possibly others as well.

Re: Sort question

Posted: Mon Apr 04, 2022 9:02 am
by richmond62
Aha:

I was looking in 'THE COMPLETE HYPERCARD 2.2 HANDBOOK'. :?

And, now you have told me what to look for . . . :D

page 541 in that books explains about mark for selective searches.

Re: Sort question

Posted: Mon Apr 04, 2022 2:28 pm
by dunbarx
Richard, Richmond.

This is entirely different. Everybody knows one can

Code: Select all

mark cards where thisIsTrue and thatIsFalse
That is a simple boolean.

What I read somewhere was strictly applied to the sort command, that created its own special compound "sortKey" that belied the on-the-face-of-it concatenation that, in fact, such a line of code seems to very standardly imply.

Whew.

Anyway, thanks for looking so hard. I did not make this up.

Craig

Re: Sort question

Posted: Mon Apr 04, 2022 7:11 pm
by RCozens
Craig, et al,

The button script in this stack will give you the result you desire in a single sort.

Re: Sort question

Posted: Mon Apr 04, 2022 8:44 pm
by dunbarx
Hi.

The handler you posted last week does this best. No kludges or custom functions, just a little automation exploiting the very powerful and stable sort command we love. One need only determine the number of words in the longest line and set the "repeat with" loop counter to that value. :wink:

I actually made a gadget that determined the longest word in a body of text, and appended all shorter words in a line with charToNum(2) so they were all of equal length. Of course, those extra padded char(s) all sorted first. A kludge and a half for sure.

But the stable sort inherent in LC (and in HC before it) makes the whole exercise unnecessary except for those who have nothing better to do than fool around with this sort of thing. Like me.

Craig

Re: Sort question

Posted: Tue Apr 05, 2022 1:39 am
by RCozens
dunbarx wrote: Mon Apr 04, 2022 8:44 pm But the stable sort inherent in LC (and in HC before it) makes the whole exercise unnecessary except for those who have nothing better to do than fool around with this sort of thing. Like me.
And moi: after posting my single sort script I realized it is unnecessary to pad the third word because it is the final element of the sort key.

This script produces the same result with four less lines of code and demonstrates how a function can be used to script custom sorting logic :

Code: Select all

local word1Max, word2Max

on mouseUp
   put field "Source Text" into text2Sort
   repeat for each line line2Sort in text2Sort
      put max(length(word 1 of line2Sort), word1Max) into word1Max
      put max(length(word 2 of line2Sort), word2Max) into word2Max
   end repeat
   sort lines of text2Sort by justify(word 1 to 3 of each)
   put text2Sort into field "Sorted Text"
end mouseUp

function justify word1,word2,word3
   repeat while length(word1) < word1Max
      put " " after word1
   end repeat
   repeat while length(word2) < word2Max
      put " " after word2
   end repeat
    return word1&word2&word3
end justify
Cheers!

Re: Sort question

Posted: Tue Apr 05, 2022 9:23 am
by rkriesel
RCozens wrote: Tue Apr 05, 2022 1:39 am This script produces the same result with four less lines of code ...
Since you're interested in less code, how about this:

Code: Select all

sort tText by pad( word 1 of each ) & pad( word 2 of each ) & word 3 of each

function pad tString
   put "" into item 1000 of tString
   replace comma with space in tString
   return char 1 to 1000 of tString
end pad
— Dick

Re: Sort question

Posted: Tue Apr 05, 2022 2:12 pm
by bn
rkriesel wrote: Tue Apr 05, 2022 9:23 am Since you're interested in less code, how about this:

Code: Select all

sort tText by pad( word 1 of each ) & pad( word 2 of each ) & word 3 of each

function pad tString
   put "" into item 1000 of tString
   replace comma with space in tString
   return char 1 to 1000 of tString
end pad
— Dick
And picking up Dick's idea how about this

Code: Select all

sort tText by ( word 1 of each ) && ( word 2 of each) && (word 3 of each) & space
It seems to work but I only have vague ideas about why...
But it does have fewer lines... :)

test case

Code: Select all

ABC XXX XYZ
AB XXX XYZ
a XXX XYZ
A XXX XYZ
Kind regards
Bernd

Re: Sort question

Posted: Tue Apr 05, 2022 2:28 pm
by dunbarx
AHA.

Bernd, my man.

Maybe I misremembered the syntax from 30 years ago. This works:

Code: Select all

sort it by word 1 of each && word 2 of each && word 3 of each
The "&&" instead of "&".

And now I (we?) have to think why, since there is still concatenation, just with a space between the several clauses.

Craig

Re: Sort question

Posted: Tue Apr 05, 2022 2:44 pm
by dunbarx
And if, after more testing and discussion, this seems to be reliable, surely a note in the dictionary is appropriate. It is, after all, adorable.

Back in the day, when users had the ability to add their own comments to the dictionary, I did indeed include this ditty in the "sort" command entry. But I do not remember if I used "&&" between clauses or not. I hope so...

Craig

Re: Sort question

Posted: Tue Apr 05, 2022 2:53 pm
by dunbarx
Once again, I feel like Richmond with my own string of posts. :D

So what is going on within the bowels of the "sort" command? This test, with a field 1 containing the alphabet soup we have been using, fooling around to see what comes up:

Code: Select all

on mouseUp
   get fld 1
   put word 1 of each && word 2 of each && word 3 of each into sortGadget
   breakpoint
   sort it by sortGadget
end mouseUp
Just to see what concatenation might turn up, right? Try it. check out the variable "sortgadget". I can see that the whole thing overall ought not to work, actually. But I do not see what is going on.

Craig

Re: Sort question

Posted: Tue Apr 05, 2022 3:47 pm
by dunbarx
Me again.

I cannot find a flaw in the new one-liner sort variant. Fooling around with various combinations of characters in all the words in all the lines of a body of text, especially using words of varying lengths within the lines themselves seems to work correctly.

I still wonder where I first read about this. I am (almost) positive it was from Goodman.

Craig

Re: Sort question

Posted: Tue Apr 05, 2022 6:53 pm
by RCozens
Morning All,

From what I'm seeing it seems that LC can sort words of different lengths correctly, and that the error initially reported was caused by concatenating the words.

So my one liner is:

Code: Select all

sort lines of text2Sort by word 1 to 3 of each
Still, I learned it is possible to code different sorting logic, which could include factors beside the text itself, by building sort keys via a function.