Sorting...

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

ValiantCuriosity
Posts: 128
Joined: Thu Jun 30, 2016 3:08 am

Sorting...

Post by ValiantCuriosity » Mon Feb 25, 2019 12:29 am

I think this is another "baby" question.

Again, just so that you don't think that I haven't attempted to find the answers to my questions before I ask them, I have searched. I've read the LC Dictionary, looked at the LC sample, read my LC book, looked on Stackoverflow, and some other places for a simple example of "sort".

As an aside: Did you know that there are 3037 messages that mention "sort" and a few less that mention "sorting" in the forum. Only a true numbers person would draw attention to that. That would NOT be me! :mrgreen:
--------------------------------------
The real question in case you don't want to waste your time on the stuff above:

I am trying to sort a list of items. I've used both the "normal" field object and the list scrolling list field object to try this out. I've found a lot of examples for various iterations of this code:

This is from the LC dictionary:

Code: Select all

 sort field "Output"
I've placed the code in my stack, in my card, in the field that I named "Output". I don't want to use an event like a button click. I just want the text to be arranged ascending. Nothing fancy. Just trying to wrap my head around some of the basic script in LC.

I've placed the text in the field's "Contents" section. Maybe that is an "it" in LC? Just wrote it into the Contents and hit return after each word.

So far, I have had no success in getting the text strings to sort when I run the program. There are a few of examples of sorting using variables/arrays, but I just wanted to keep it simple.

What am I doing wrong? Brain fog number 1,999,999. Please save me from 2,000,000.

-Rachel
May I never be cured of my curiosity! :D

SparkOut
Posts: 2852
Joined: Sun Sep 23, 2007 4:58 pm

Re: Sorting...

Post by SparkOut » Mon Feb 25, 2019 1:01 am

ValiantCuriosity wrote:
Mon Feb 25, 2019 12:29 am
I don't want to use an event like a button click. I just want the text to be arranged ascending.
Hi Rachel
So how are you thinking the sorting handler will be triggered? There always has to be an event that generates the message to trigger the handler, whether that is caused by script or by human interaction. You mean you just want whatever text is in the field to be automatically sorted whenever the contents change? Aha, then the script of the field could handle the "textChanged" message. But that could be very disconcerting if the user is typing in the field, the textChanged message will fire as each letter is typed and cause the sorting to occur from the very first letter.
Perhaps when the editing is done, the "exitField" or "closeField" message (or both of these) are better to handle for the sort to be triggered. Is this more along the lines of what you were thinking?
Last edited by SparkOut on Mon Feb 25, 2019 1:09 am, edited 1 time in total.

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

Re: Sorting...

Post by dunbarx » Mon Feb 25, 2019 1:08 am

The good news is that sorting is very simple, powerful, fast and straightforward.

We will need a bit of clarity. Small details matter.

You have a list of "items"? "Item" means something quite specific in LC, and we do not want to misuse it. Does your list something look like this?

monkey
cat
dog

I am not being pedantic, but there are no items in the above list. The reason I mention it is because it is possible to sort lines (suitable for the above) as well as items, as in "monkey, cat,dog", and the result of the sort will look quite different.
I just want the text to be arranged ascending.
By "arranged", since you mention you do not want to invoke a sort with a button handler, how then do you want to do it? Automatically after each entry (or deletion) of each line by user input? In other words, is your issue with invoking "sort", or that the sorting itself is not giving you what you want?
I have had no success in getting the text strings to sort when I run the program.
Hmmm. See my paragraph immediately above the quote.

Have you had success experimenting with a simple field (or table field) and sorting its contents? If you place the animal list above in a field, and sort it, does it work? I assume it does. unless there are hidden issues with your data. I want to make sure you have sorted something somewhere, so we can dig deeper into the problem you are currently having.

Write back.

Craig Newman

ValiantCuriosity
Posts: 128
Joined: Thu Jun 30, 2016 3:08 am

Re: Sorting...

Post by ValiantCuriosity » Mon Feb 25, 2019 2:26 am

Hi guys,

I think maybe I'm not explaining this well.

When I mention a list in a field, I mean a field that doesn't get any input. The input is turned off. When the card opens, I wanted to sort the items. ( Craig, how did you know my sample list is a list of animals? My list sample was a little different, but dog, cat... is a part to it!) :D

So, basically, I want to "sort" a no input Field List or no input Field with text content on each line when the app opens. I've unchecked/unticked the lock on the field input.

I suppose on preOpenCard or preOpenStack might be where I want to put the code. I am not sure. I've tried it. It doesn't work.

I would like the list to be sorted when it appears on the card in the field. The text that I'm using was entered in the Field Content area of LiveCode.
So:
1. I have a list that is "Dog, Cat, Rabbit, Monkey".
2. When I open the card,
3. I want to sort the list in alphabetical order (ascending)

When the card opens, I'd like it to show "Cat, Dog, Monkey, Rabbit"

Thank you for trying to help me understand how to use this command.
-Rachel
May I never be cured of my curiosity! :D

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

Re: Sorting...

Post by dunbarx » Mon Feb 25, 2019 3:10 am

Your problem lies somewhere else, since you mentioned something similar to:

Code: Select all

on openCard
  sort fld "animals"
end openCard
This will work without issue, provided that the handler fires. So might the problem lie with the location of the handler itself? Is it explicitly in the card that contains the field to be sorted? If not, do you know that placing it in the stack script would cover all bases, assuming there is a field "animals" in any card that you navigate to?

We are getting closer...

Craig

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Sorting...

Post by bogs » Mon Feb 25, 2019 10:01 am

ValiantCuriosity wrote:
Mon Feb 25, 2019 12:29 am
What am I doing wrong?
Probably your clean living and unwholesome healthiness. You really should stay out of direct sunlight whenever possible and adjust slowly and surely to the flickering flourescen... oh wait, that was someone else, never mind.

As was said previously, if you didn't get the field to sort under the conditions you mentioned, then something else must be affecting it, OR, possibly you didn't see it sort in the way you expected.

This is a simple demo, sorts a list ascending, pops up an answer dialog so you can look at the list :wink: then sorts it descending, and lastly you get a surprise BONUS sort, absolutely FREE, thats right, for NO MONEY WHATsoever, we went crazy and threw in a really time consuming sort that would confound the MUSES of the AGES, BUT THATS NOT ALL!!!

Well, ok, that is all really. I got nothing.
SortDemo.zip
Sorted out...
(731 Bytes) Downloaded 172 times
When the card opens, I'd like it to show "Cat, Dog, Monkey, Rabbit"
This is what Craig was mentioning earlier on knowing exactly what your sorting.

If your field has all those items as shown, separated by commas, then you would need to sort the items of the field, by default, sort takes lines as Craig was pointing out.

Still waking up, sue me Image
Image

Klaus
Posts: 13824
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Sorting...

Post by Klaus » Mon Feb 25, 2019 2:36 pm

Hi all,

"sort fld xyz" always does a LINE sort (like -> sort LINES of fld xyz), unless you tell LC otherwise.
You want:
...
sort ITEMS of fld "your field here"
...

Best

Klaus

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

Re: Sorting...

Post by dunbarx » Mon Feb 25, 2019 3:44 pm

So, do you have a list of lines or a list of items?

Have you tried the simple experiment I mentioned, or what is likely something similar in Bogs' sample?

Like I said, we are getting close. We need you to close the gap.

Craig

ValiantCuriosity
Posts: 128
Joined: Thu Jun 30, 2016 3:08 am

Re: Sorting...

Post by ValiantCuriosity » Tue Feb 26, 2019 1:08 am

Bogs,

That little demo is just so KOOL! Thank you. Exciting stuff. :D The code looks pretty similar to what I was trying. The only difference was that I named the field.

Code: Select all

sort Field "MyField" ascending
When I first load your demo to LC, it runs. (Stack initially loads) When I put the demo into edit mode and then, again, into run mode again, nothing happens. I must be missing some kind of "refresh". I thought that LC just re-ran the app when the "Browse Tool" was selected and stopped when it wasn't selected.I think this may be what Craig meant when he talked about some other issue than the sort code.

Craig, I appreciate your help. I need to get the vocabulary down so that you can all understand what I'm asking. What I'm doing right now is trying to go through some of the commands in LC to understand how they work. Overall, I am enjoying LC and hope that I can eventually get over my brain freeze.

You guys are all so helpful.

P.S. "Drag me" is kind of fun and SO easy. Not so easy an achievement for me in javascript!

-Rachel
May I never be cured of my curiosity! :D

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

Re: Sorting...

Post by dunbarx » Tue Feb 26, 2019 4:04 am

Hi.

Place a breakpoint at the "sort" line. That is what I meant: does the issue lie with the line itself not executing?

You must learn to do this, and step through your handlers line by line. That method tells you not only where problems are, but also if the handler fires at all.

Write back...

Craig

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Sorting...

Post by bogs » Tue Feb 26, 2019 11:50 am

ValiantCuriosity wrote:
Tue Feb 26, 2019 1:08 am
When I first load your demo to LC, it runs. (Stack initially loads) When I put the demo into edit mode and then, again, into run mode again, nothing happens. I must be missing some kind of "refresh". I thought that LC just re-ran the app when the "Browse Tool" was selected and stopped when it wasn't selected.I think this may be what Craig meant when he talked about some other issue than the sort code.
Indeed. The handler is in the 'Stack' script, and fires with the 'openCard' message. The card only opens one time though :D there are a couple of ways you can proceed.

If you want to run it again without changing anything, open the code editor to the script for the stack, then click on the green arrow ~
Selection_004.png
Green Arrow !
It will ask you which handler to run (you only have one handler now, larger projects will show that more usefully), and it will run again.

Another way you can do this is to change the word 'openCard' to 'mouseUp' (make sure you change both). Then you can run it any time you click on the card (but not in the field) :D

You can add the breakpoint Craig is talking about by either clicking in the gutter of the script editor (left of where the code is) and you'll see a red dot next to it, or by just typing the word 'breakpoint' on the line after the line of code you want it to start debugging at.

Using the type in method is very handy, theoretically it will only fire when in the IDE and not in a standalone, but it is still a good practice to get rid of breakpoints when your releasing your programs :wink:
Image

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7229
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Sorting...

Post by jacque » Tue Feb 26, 2019 5:35 pm

The quickest way to test a handler without fiddling with the script editor at all is to just type the handler name into the message box and hit the return key. The message box is your command line tool. Every handler needs a trigger and you can provide that via command line, in this case just type "openstack" and the handler will run, provided the stack in question has focus. (This is invaluable for testing messages for mobile apps that aren't sent on desktop.)

You can see what stack the message box will operate on by looking at its title field. You can also change its target stack by clicking the title field and choosing a different one, provided you have more than one open.

Commands from the message box are sent to the current card, so if you need to test button or field scripts you need to use the "send" command to send to those. This isn't needed too often since you can just click the button or type in the field instead, but it's good to be aware of that.

There are other panes in the message box that do other things. It's one of the most useful tools we have.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Sorting...

Post by bogs » Tue Feb 26, 2019 11:03 pm

jacque wrote:
Tue Feb 26, 2019 5:35 pm
...in this case just type "openstack" and the handler will run, provided the stack in question has focus. (This is invaluable for testing messages for mobile apps that aren't sent on desktop.)
<sic>
Commands from the message box are sent to the current card, so if you need to test button or field scripts you need to use the "send" command to send to those.
Erm, I think for openStack you'd have to 'send openStack to the mouseStack" from the message box, wouldn't you?

After all, if you type in the MB, the other stack doesn't have focus, and it would be pretty hard to hit enter for the MB if the other stack has focus at the time ... oh dear, my eyes have gone crossed again Image
Image

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7229
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Sorting...

Post by jacque » Wed Feb 27, 2019 1:08 am

Erm, I think for openStack you'd have to 'send openStack to the mouseStack" from the message box, wouldn't you?
Nope. The message box doesn't send messages to itself, it aims for the defaultstack -- except when LC goes wacky, that is, but then there are other problems. ;)
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Sorting...

Post by bogs » Wed Feb 27, 2019 11:43 am

See Rachel? Don't get too set into doing things one way, or your focus may become to myopic, like mine Image
Image

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”