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: 9655
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Sort question

Post by dunbarx » Wed Mar 30, 2022 11:26 pm

This came from a thread on the useless. Sorry, use-list.

It is an old, old technique, HC-old, that one can perform compound stable sorts of the form, for example:

Code: Select all

 sort lines of tList by word 1 of each & word 2 of each & word 3 of each
Given the following in tList:

Code: Select all

ABC DEF XYZ
ABD DEF XYZ
ABC BCD XYZ
AAA XYZ ZZZ
One gets:

Code: Select all

AAA XYZ ZZZ
ABC BCD XYZ
ABC DEF XYZ
ABD DEF XYZ
Well and good. BUT if one has the following:

Code: Select all

ABC XXX XYZ
AB XXX XYZ
Sorting gives the same list right back at you. "AB" should sort before "ABC" but is does not.

??

Craig

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

Re: Sort question

Post by richmond62 » Thu Mar 31, 2022 7:48 am

Possibly because LiveCode sees the space after AB as something further down the list than C.

I know that that is bonkers as the ASCII/Unicode for space is normally 32 (hex 0020), and the code for
capital C is 67 (hex 0043) . . .

. . . just starting on my second cup of coffee . . . 8)

. . . I'll try to think of a more intelligent reason once the caffeine hits. :D

Mind you . . . how about sorting by charToCodePoint . . . ? :twisted:

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

Re: Sort question

Post by richmond62 » Thu Mar 31, 2022 8:10 am

Oh, Cheese!

Tried this:

Code: Select all

 set the itemDelimiter to " "
 sort lines of tList by item 1 of each & item 2 of each & item 3 of each
and that did NOT work either.

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

Re: Sort question

Post by richmond62 » Thu Mar 31, 2022 8:15 am

Screen Shot 2022-03-31 at 10.14.26 AM.png
-
That works . . . :D
-
As does this:

Code: Select all

on mouseUp
   put empty into fld "daftLIST2"
   put fld "daftLIST" into BONKERS
   sort lines of BONKERS
   put BONKERS into fld "daftLIST2"
end mouseUp
Someone, somewhere, might have been over-complicating things . . . :?

[Way of getting away from Richmond's obsession with fields.]
-
Attachments
BAD NEWS.livecode.zip
Stack.
(1.19 KiB) Downloaded 67 times

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

Re: Sort question

Post by dunbarx » Thu Mar 31, 2022 2:04 pm

Richmond.

The issue is how the compound sort gadget:

Code: Select all

  sort lines of it by word 1 of each & word 2 of each & word 3 of each
either works or does not work. If you simply sort the list:

Code: Select all

ABC XXX XYZ
AB XXX XYZ
It works fine. The "AB" goes to the top, because a blank always sorts before any alpha character ("AB" comes before "ABC"). But with the compound sort it does not. I have used this for decades, and only now am finding that it may not be universally sound.

Craig

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

Re: Sort question

Post by FourthWorld » Thu Mar 31, 2022 2:50 pm

What do you see when you run this?:

Code: Select all

put line 1 of tList into tLine
put word 1 of tLine & word 2 of tLine & word 3 of tLine
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

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

Re: Sort question

Post by dunbarx » Thu Mar 31, 2022 3:49 pm

Richard.

With "ABC XXX XYZ" I get what I would have thought, that is, "ABCXXXXYZ".

Craig

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

Re: Sort question

Post by FourthWorld » Thu Mar 31, 2022 4:03 pm

So when you concatenate this:

Code: Select all

ABC XXX XYZ
AB XXX XYZ
...what gets sorted is:

Code: Select all

ABCXXXXYZ
ABXXXXYZ
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

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

Re: Sort question

Post by RCozens » Thu Mar 31, 2022 6:00 pm

This will give you the result you expect:

Code: Select all

on mouseUp
   put empty into fld "daftLIST2"
   put fld "daftLIST" into text2Sort
   repeat with word2Sort = 3 to 1 step -1
      sort lines of text2Sort by word word2Sort of each
   end repeat
   put text2Sort into field "daftLIST2"
end mouseUp
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.

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

Re: Sort question

Post by dunbarx » Thu Mar 31, 2022 7:52 pm

Richard.

I see what you mean. I always thought that was a special variant of "sort" and was designed to leave the words involved intact. I was obviously wrong, and the way you explain it, there was never any special "variant" at all. Well. That means that the method requires that all words be of the same length. It has been years since the last time I used this, er, sort of thing, but I almost cannot believe that for so long it has not broken something simple.

Anyway. I can think of kludges to "fix" it, like temporarily inserting low-ASCII chars to fill the voids, but that does not seem worthwhile at all.

Thanks.

Craig

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

Re: Sort question

Post by dunbarx » Thu Mar 31, 2022 8:02 pm

Rob.

Yes, and nice. :D

Get rid of the concatenation. Again, something I thought was dealt with internally in that form of the command variant.

Why so odd a repeat, though? Why not just:

Code: Select all

  repeat with word2Sort = 3 down to 1
Not that it matters...

Craig

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

Re: Sort question

Post by FourthWorld » Thu Mar 31, 2022 8:38 pm

dunbarx wrote:
Thu Mar 31, 2022 7:52 pm
Richard.

I see what you mean. I always thought that was a special variant of "sort" and was designed to leave the words involved intact. I was obviously wrong, and the way you explain it, there was never any special "variant" at all. Well. That means that the method requires that all words be of the same length. It has been years since the last time I used this, er, sort of thing, but I almost cannot believe that for so long it has not broken something simple.
I double-checked "HyperTalk 2.2: The Book" and there's no multi-sort variant there either. Then I tried LC using the logical "and" rather than the concatenating "&" - still no go (though oddly it doesn't throw an error for the unused "and..." clause). Like the HC docs, the LC Dictionary has no example of multi-sort expression options.

It's a nice idea, but does not appear to have been implemented in an xTalk before.
Anyway. I can think of kludges to "fix" it, like temporarily inserting low-ASCII chars to fill the voids, but that does not seem worthwhile at all.
Given that LC uses a stable sort, no need to alter the data. Just do multiple sorts one at a time and you'll get exactly what you're looking for.
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: 9361
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Sort question

Post by richmond62 » Thu Mar 31, 2022 8:51 pm

The issue is how the compound sort gadget
OK, I apologise: missed that bit.

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

Re: Sort question

Post by dunbarx » Thu Mar 31, 2022 9:20 pm

I did not make that up. But in my rather extensive HC library, I could not find it either. It must be some aside that is not linked to "sort" in the index.

Craig

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

Re: Sort question

Post by richmond62 » Fri Apr 01, 2022 9:53 am

OK, OK, OK . . .

I have Danny G's HC 2.2 book on my lap and Danny G's Hypercard Developers Guide 1988 as a PDF up on a screen.

The latter one mentions "Dual Key" Sorts, but that seems to refer to 2 fields . . . threw me into the left field as Goodman's
style is anything if not clunky.

Certainly the former's Chapter 29 on Searching and Sorting does not mention anything quite resembling the

sort by word 1 and word2 and word3 sort of stuff. :?

Post Reply

Return to “Talking LiveCode”