Sort question

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

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9658
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Sort question

Post by dunbarx » Sun Apr 03, 2022 10:38 pm

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

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9833
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Sort question

Post by FourthWorld » Sun Apr 03, 2022 11:03 pm

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?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9833
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Sort question

Post by FourthWorld » Sun Apr 03, 2022 11:14 pm

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.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9385
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Sort question

Post by richmond62 » Mon Apr 04, 2022 9:02 am

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.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9658
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Sort question

Post by dunbarx » Mon Apr 04, 2022 2:28 pm

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

RCozens
Posts: 138
Joined: Thu Aug 05, 2021 6:42 am
Location: Manchester, CA USA

Re: Sort question

Post by RCozens » Mon Apr 04, 2022 7:11 pm

Craig, et al,

The button script in this stack will give you the result you desire in a single sort.
Attachments
Single Sort.zip
(909 Bytes) Downloaded 64 times
Rob Cozens dba Serendipity Software Company
Manchester, CA USA

Each new generation gives more attention to the man-made world...
and less attention to the world that made man.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9658
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Sort question

Post by dunbarx » Mon Apr 04, 2022 8:44 pm

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

RCozens
Posts: 138
Joined: Thu Aug 05, 2021 6:42 am
Location: Manchester, CA USA

Re: Sort question

Post by RCozens » Tue Apr 05, 2022 1:39 am

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!
Rob Cozens dba Serendipity Software Company
Manchester, CA USA

Each new generation gives more attention to the man-made world...
and less attention to the world that made man.

rkriesel
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 118
Joined: Thu Apr 13, 2006 6:25 pm

Re: Sort question

Post by rkriesel » Tue Apr 05, 2022 9:23 am

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

bn
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4000
Joined: Sun Jan 07, 2007 9:12 pm
Location: Bochum, Germany

Re: Sort question

Post by bn » Tue Apr 05, 2022 2:12 pm

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9658
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Sort question

Post by dunbarx » Tue Apr 05, 2022 2:28 pm

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
Last edited by dunbarx on Tue Apr 05, 2022 2:45 pm, edited 2 times in total.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9658
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Sort question

Post by dunbarx » Tue Apr 05, 2022 2:44 pm

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9658
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Sort question

Post by dunbarx » Tue Apr 05, 2022 2:53 pm

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

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9658
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Sort question

Post by dunbarx » Tue Apr 05, 2022 3:47 pm

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

RCozens
Posts: 138
Joined: Thu Aug 05, 2021 6:42 am
Location: Manchester, CA USA

Re: Sort question

Post by RCozens » Tue Apr 05, 2022 6:53 pm

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.
Rob Cozens dba Serendipity Software Company
Manchester, CA USA

Each new generation gives more attention to the man-made world...
and less attention to the world that made man.

Post Reply

Return to “Talking LiveCode”