Sort multidimensional array

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

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

Re: Sort multidimensional array

Post by jacque » Fri Mar 05, 2021 7:36 pm

Would a custom sort function help? LC doesn't require scripts to use only the built-in options.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

stam
Posts: 2679
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Sort multidimensional array

Post by stam » Fri Mar 05, 2021 7:47 pm

jacque wrote:
Fri Mar 05, 2021 7:36 pm
Would a custom sort function help? LC doesn't require scripts to use only the built-in options.
Hi Jacque - any method for sorting textual list of dates correctly would fix the issue ;)

But ideally the internal command sort container would work as advertised...

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 multidimensional array

Post by FourthWorld » Fri Mar 05, 2021 7:51 pm

stam wrote:
Fri Mar 05, 2021 7:26 pm
Hi Richard,
FourthWorld wrote:
Fri Mar 05, 2021 6:28 pm
stam wrote:
Fri Mar 05, 2021 9:19 am
Thanks Richard -- while i completely agree, that also misses the point of my question a bit
Did it?

Let's see:
Alas, that was not my question.
I fear the speaking of 'arrays' has clouded the issue. What doesn't work is sorting a textual list of dates.

The issue is that

Code: Select all

sort [{lines | items} of] <container> [<direction>] [<sortType>] [by <sortKey> ]
seems to fail on sortType of dateTime
...
So i'll have to disagree, it didn't address my question - which is why sorting a container specifically by dateTime fails, when it works fine for numeric/text, and if this is a bug?
You might want to read the rest of my post. It covers, among other things, why useSystemDate fails in that context.

My post contains not only the fish, but a fishing pole, a boat, a jar of worms, an assortment of tackle, and a field guide to the breeding and eating habits of fish, all tidily packed into just a few paragraphs with a bow on top.

If anticipating these sorts of things in the future isn't of interest and all you want is the fish, see the "international" sort option.


Tip for catching fish: When you have a question unrelated to arrays, choosing a thread explicitly titled as being about arrays as the place to insert it, and repeatedly referring to examples that rely on arrays exclusively, may prompt respondents to believe you have an interest in arrays. ;)
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

stam
Posts: 2679
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Sort multidimensional array

Post by stam » Fri Mar 05, 2021 8:34 pm

FourthWorld wrote:
Fri Mar 05, 2021 7:51 pm
Tip for catching fish: When you have a question unrelated to arrays, choosing a thread explicitly titled as being about arrays as the place to insert it, and repeatedly referring to examples that rely on arrays exclusively, may prompt respondents to believe you have an interest in arrays. ;)
Completely my bad, i misunderstood your comment on useSystemDate - i just assumed that if useSystemDate was called within a handler it would be considered local to and apply to any called function from said hander. Very bad assumption on my part!
Adding this in the downstream sort function fixes the issue -- colour me embarrassed :oops: :oops:

As for catching fish - i didn't choose this thread, it was just a natural continuation (for me anyway!) of the lengthy discussion, and I assumed everyone was on my wavelength, only it seems my wavelength was far, far left field ;)

Thanks as always for your wisdom :)

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

Re: Sort multidimensional array

Post by bogs » Fri Mar 05, 2021 8:48 pm

stam wrote:
Fri Mar 05, 2021 8:34 pm
I assumed everyone was on my wavelength, only it seems my wavelength was far, far left field
Not so much, I measured your wavelength at 48 ... oops, my ride is here
Image
Image

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 multidimensional array

Post by FourthWorld » Fri Mar 05, 2021 8:57 pm

stam wrote:
Fri Mar 05, 2021 8:34 pm
Completely my bad, i misunderstood your comment on useSystemDate...
No worries. I've seen enough of you posts that I have a good sense of the solid place you're coming from, and TBH you've been so helpful here that in my book you can do no wrong. Indeed, the other day you were in a similar situation yourself. ;)

There's a lot going on in these forums, and threads here tend to sprawl far beyond the OP's imagination. We all miss details now and then (Klaus can list the hundred or so times I have).

Those looking for quick answers in a heavily-moderated venue tightly honed for that are already on StackOverflow.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

bobcole
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 135
Joined: Tue Feb 23, 2010 10:53 pm
Location: Saint Louis, Missouri USA

Re: Sort multidimensional array

Post by bobcole » Sat Mar 06, 2021 4:27 am

Looking at the original dates makes me wonder if they aren't in the right format.
The 1999 dates are in DD/MM/YY format where the 20xx dates are in MM/DD/YYYY format.
The 20xx date sort correctly. The 1999 dates are not sorted as desired.
Might the date format for the 1999 dates be the source of the problem?
Just a thought...
Bob

bobcole
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 135
Joined: Tue Feb 23, 2010 10:53 pm
Location: Saint Louis, Missouri USA

Re: Sort multidimensional array

Post by bobcole » Sat Mar 06, 2021 4:32 am

Oops, I meant the 19xx dates are in DD/MM/YYYY format
and the 20xx dates dates are in MM/DD/YYYY format.
It seems that LC doesn't understand the 19xx dates fully but it does know to put them at the top of the list based on the century.
Bob

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 multidimensional array

Post by FourthWorld » Sat Mar 06, 2021 6:00 am

bobcole wrote:
Sat Mar 06, 2021 4:32 am
Oops, I meant the 19xx dates are in DD/MM/YYYY format
and the 20xx dates dates are in MM/DD/YYYY format.
Maybe. It depends whether "07/11/2011" refers to July 11 or Nov 7.

Though all of the 20xx dates in that example can seem ambiguous since they use numbers lower than 13 in both the first and second places, Stam seems clear that the full set is in non-US format.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

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

Re: Sort multidimensional array

Post by bogs » Sat Mar 06, 2021 12:06 pm

It has to be something like that, I was able to sort these two fields by dateTime (after I figured out I goofed previously)....

Image

It also appears to have correctly sorted by dateTime when the two lists are combined, note the hours especially...

Image

Even though they are a mixed format, they *are* in the correct order.
Image

bobcole
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 135
Joined: Tue Feb 23, 2010 10:53 pm
Location: Saint Louis, Missouri USA

Re: Sort multidimensional array

Post by bobcole » Sun Mar 07, 2021 6:29 am

the 19xx dates are in DD/MM/YYYY format
and the 20xx dates dates are in MM/DD/YYYY format.
It seems that LC doesn't understand the 19xx dates fully but it does know to put them at the top of the list based on the century.
I tried converting the six dates provided into dateItems. The 20xx dates converted correctly. the 19xx dates did not convert at all. The dates need to be in the same format (based on the locale?) so LC can interpret them correctly.

Converting the dates or times provided by bogs to dateItems helps understand how LC treats them.
Try running this sample statement block on various dates or times:

Code: Select all

put "Sat,Mar 6, 2021" into tValue
convert tValue to dateItems
put tValue & return into message box
convert tValue to long date and abbreviated time
put tValue & return after message box
My two cents,
Bob

stam
Posts: 2679
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Sort multidimensional array

Post by stam » Sun Mar 07, 2021 1:26 pm

bobcole wrote:
Sun Mar 07, 2021 6:29 am
I tried converting the six dates provided into dateItems. The 20xx dates converted correctly. the 19xx dates did not convert at all. The dates need to be in the same format (based on the locale?) so LC can interpret them correctly.
Hi Bob,
The 6 dates are in UK format (dd/mm/yyyy - i think i actually mentioned that in one of the preceding posts). On further testing, the sorting did work with American formatting (mm/dd/yyyy).
Thanks to Richard, I realised i was setting the set the useSystemDate to true in the wrong place - in the handler calling the sort function, instead of actually inside the sortArray function. Doing the latter fixes the issue.

I tried to abstract the sortArray function as much as possible so only single function can be called no matter if 1-dimensional or multidimensional or what sortDirection/Type, but I hadn't factored in the requirement for date formatting/locale. I've changed my function to include an additional parameter for the useSystemDate, which is now called inside the function.

So this one function should now return a sorted array regardless the nature of the array. Only required parameter is the array. Other params can be set as empty - pDirection will default to ascending, pSortType will default to text, pUseSystemDate will default to true; leaving pKey empty will treat the array as 1-dimensional.

This works well for me now:

Code: Select all

function sortArray @pArray, pDirection, pSortType, pKey, pUseSystemDate
   local tNextIndex, tSortedArray, tSortText
   get the keys of pArray
   set the useSystemDate to pUseSystemDate
   If pUseSystemDate is empty then set the useSystemDate to true // if not provided, default to user's environment
   put "sort lines of it [[pDirection]] [[pSortType]] by pArray[each]" into tSortText //1-dimensional array
   if pKey is not empty then put "[pKey]" after tSortText //multidimensional array
   do merge(tSortText)
   repeat for each line tKey in it
      add 1 to tNextIndex
      put pArray[tKey] into tSortedArray[tNextIndex]
   end repeat
   return tSortedArray
end sortArray

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 multidimensional array

Post by FourthWorld » Sun Mar 07, 2021 4:32 pm

Once the function returns to me this array described as "sorted", what would I expect to see when I obtain its keys?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

stam
Posts: 2679
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Sort multidimensional array

Post by stam » Sun Mar 07, 2021 7:19 pm

FourthWorld wrote:
Sun Mar 07, 2021 4:32 pm
Once the function returns to me this array described as "sorted", what would I expect to see when I obtain its keys?
It returns a numerically keyed array with the same content of the keys in the source array, but ordered as desired...

For other readers, I attach an example - arrays shown with tree view widgets, unfortunately I haven't figured out how to expand all in script so needs to be done manually (set the foldState doesn't seem to work... if you know how to expand all branches of the tree view in script do let me know!).

It's really just a generalised abstraction of the lesson on the same topic, which means i can use only 1 function for all my sorting needs.
Unless you're referring to something else or hinting at an issue that's going way over my head? (very easily done!).



As a sidenote: there is a slight error in my code above, it doesn't like the pUseSystemDate passed as empty as it first tries to assign the boolean, so i've changed the 2 lines of code

Code: Select all

set the useSystemDate to pUseSystemDate
If pUseSystemDate is empty then set the useSystemDate to true
to

Code: Select all

if pUseSystemDate is not empty then set the useSystemDate to pUseSystemDate
empty will default to US format instead of the user's format, but hey ho... (i know i could do a conditional for the reverse, but I'm averse to adding more lines to code...)
Attachments
sortArrayExample.livecode.zip
(1.9 KiB) Downloaded 147 times

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 multidimensional array

Post by FourthWorld » Sun Mar 07, 2021 9:16 pm

stam wrote:
Sun Mar 07, 2021 7:19 pm
FourthWorld wrote:
Sun Mar 07, 2021 4:32 pm
Once the function returns to me this array described as "sorted", what would I expect to see when I obtain its keys?
It returns a numerically keyed array with the same content of the keys in the source array, but ordered as desired...
What do you see when you run this?:

Code: Select all

on mouseup
   -- build array:
   put "Carol" into tA[1]["name"]
   put "Jane"  into tA[2]["name"]
   put "Bob"   into tA[3]["name"]
   put "Steve" into tA[4]["name"]
   put "Al"    into tA[5]["name"]
   put "Tina"  into tA[6]["name"]
   put "Kim"   into tA[7]["name"]
   put "Dude"  into tA[8]["name"]
   put "Chris" into tA[9]["name"]
   put "Liz"  into tA[10]["name"]
   --
   -- "sort" it:
   put sortArray(tA, "ascending", "text", "name", empty) into tSortedA
   --
   -- observe the results:
   put the keys of tSortedA
end mouseup
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”