Sort question
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
-
- VIP Livecode Opensource Backer
- Posts: 9658
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Sort question
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
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
-
- VIP Livecode Opensource Backer
- Posts: 9833
- Joined: Sat Apr 08, 2006 7:05 am
- Location: Los Angeles
- Contact:
Re: Sort question
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?
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
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
-
- VIP Livecode Opensource Backer
- Posts: 9833
- Joined: Sat Apr 08, 2006 7:05 am
- Location: Los Angeles
- Contact:
Re: Sort question
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.
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
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
-
- Livecode Opensource Backer
- Posts: 9385
- Joined: Fri Feb 19, 2010 10:17 am
- Location: Bulgaria
Re: Sort question
Aha:
I was looking in 'THE COMPLETE HYPERCARD 2.2 HANDBOOK'.
And, now you have told me what to look for . . .
page 541 in that books explains about mark for selective searches.
I was looking in 'THE COMPLETE HYPERCARD 2.2 HANDBOOK'.
And, now you have told me what to look for . . .
page 541 in that books explains about mark for selective searches.
-
- VIP Livecode Opensource Backer
- Posts: 9658
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Sort question
Richard, Richmond.
This is entirely different. Everybody knows one can
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
This is entirely different. Everybody knows one can
Code: Select all
mark cards where thisIsTrue and thatIsFalse
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
Craig, et al,
The button script in this stack will give you the result you desire in a single sort.
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.
Manchester, CA USA
Each new generation gives more attention to the man-made world...
and less attention to the world that made man.
-
- VIP Livecode Opensource Backer
- Posts: 9658
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Sort question
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.
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
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.
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
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
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.
Manchester, CA USA
Each new generation gives more attention to the man-made world...
and less attention to the world that made man.
Re: Sort question
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
-
- VIP Livecode Opensource Backer
- Posts: 4000
- Joined: Sun Jan 07, 2007 9:12 pm
- Location: Bochum, Germany
Re: Sort question
And picking up Dick's idea how about thisrkriesel wrote: ↑Tue Apr 05, 2022 9:23 amSince you're interested in less code, how about this:— DickCode: 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
Code: Select all
sort tText by ( word 1 of each ) && ( word 2 of each) && (word 3 of each) & space
But it does have fewer lines...
test case
Code: Select all
ABC XXX XYZ
AB XXX XYZ
a XXX XYZ
A XXX XYZ
Bernd
-
- VIP Livecode Opensource Backer
- Posts: 9658
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Sort question
AHA.
Bernd, my man.
Maybe I misremembered the syntax from 30 years ago. This works:
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
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
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.
-
- VIP Livecode Opensource Backer
- Posts: 9658
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Sort question
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
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
-
- VIP Livecode Opensource Backer
- Posts: 9658
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Sort question
Once again, I feel like Richmond with my own string of posts.
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:
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
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
Craig
-
- VIP Livecode Opensource Backer
- Posts: 9658
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Sort question
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
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
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:
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.
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
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.
Manchester, CA USA
Each new generation gives more attention to the man-made world...
and less attention to the world that made man.