Sorting a 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

Post Reply
buchacho
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 50
Joined: Fri Jun 14, 2013 10:22 pm

Sorting a multidimensional array

Post by buchacho » Tue Jul 16, 2013 3:00 am

I am not sure how to approach this problem. I have an array that is something like this:

Code: Select all

"C1"    "C2"     "C3"
_________________________
A      1E012      01-A
A      1E012      1B-A
A      1E012      35-B
A      1E012      35-C
Z      1E043      0X-B
A      1E212      36-A
A      1E234      2B-A
A      1E234      3B-A
A      1E234      35-A
I would like to first sort the column C2 in numerical order. It can just be ordered by the last 3 digits if the mix of letters and numbers is an issue since the first 2 characters do not change, but if it can sort the entire alpha-numeric that would be great. Then I would like to further sort within each common group in column C2 by column C3 by the first char, then the second, then the third. Column C1 does not need to be sorted.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10331
Joined: Wed May 06, 2009 2:28 pm

Re: Sorting a multidimensional array

Post by dunbarx » Tue Jul 16, 2013 3:44 am

Hi.

Convert the array into an ordinary variable with the "combine" command. Sort from there. I put your text into field 1. Note that multiple stable sorts may be effected in one line as:

Code: Select all

on mouseUp
   get fld 1
   replace numtochar(202) with comma in it
   
  sort it  numeric  by char 6 of item 4 of each & char 1 of item 7 of each & char 2 of item 3 of each & char 4 of item 7 of each
end mouseUp
Now in your text, you have a lot of weird stuff. I had to dig around to find it. That is why I changed the invisible chars you had (charToNum(202)) to a comma, just so I could see them. That is why the items seem out of order with the visible representation of your text. Also, you have a space before the second (ostensible) item in each line. I assume all this makes sense to your particular data.

Craig Newman

buchacho
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 50
Joined: Fri Jun 14, 2013 10:22 pm

Re: Sorting a multidimensional array

Post by buchacho » Tue Jul 16, 2013 5:59 pm

Craig, thanks for your help. I have not tried it yet, but wanted to clarify that the table I presented was an example to help visually clarify the arrangement of data. It could also be in the form of:

Code: Select all

arrayToSort[1]["C1"] = "A"
arrayToSort[1]["C2"] = "1E012"
arrayToSort[1]["01-A"] = "01-A"
Also, I just typed that table here in the forum webpage, so I am not sure why the formatting has weird characters. There should just be spaces between the columns.

But the array does eventually get put into a field and/or text file in a certain format with spaces for another system to read the contents in another system. So sorting within a field might be easier, but I might also need help to figure out how to sort the array. I am going to try your sample script with a field....

buchacho
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 50
Joined: Fri Jun 14, 2013 10:22 pm

Re: Sorting a multidimensional array

Post by buchacho » Tue Jul 16, 2013 8:21 pm

I played with some code and found if I use:

Code: Select all

sort fld "Field1"  by word 2 of each & word 3 of each
with the field:

Code: Select all

A      1E012      01-A
A      1E012      1B-A
A      1E012      35-B
A      1E012      35-C
Z      1E043      0X-B
A      1E212      36-A
A      1E234      2B-A
A      1E234      3B-A
A      1E234      35-A
It sorts OK. However, the data is actually organized like this:

Code: Select all

A C    1E012      01-A
A      1E012      1B-A
 B      1E012      35-B
A      1E012      35-C
  C    1E043      0X-B
A      1E212      36-A
ABC    1E234      2B-A
A      1E234      3B-A
  C    1E234      35-A
So using words to find the right location in the line to sort by is not reliable. I do not care about sorting by the first column on the left with letters. The first char of columns 2 and 3 always start at the same char number in the line.

I tried:

Code: Select all

sort fld "Field1"  numeric by char 8 of each & char 9 of each &  char 10 of each &  char 11 of each & char 12 of each
On Field1 containing:

Code: Select all

A      1E012      35-C
  C    1E043      0X-B
A      1E212      36-A
ABC    1E234      2B-A
A      1E234      3B-A
  C    1E234      35-A
A C    1E012      01-A
A      1E012      1B-A
B      1E012      35-B
But nothing happened.

Also, is it possible to restrict the sort to a certain portion of the table, like lines x to y?

buchacho
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 50
Joined: Fri Jun 14, 2013 10:22 pm

Re: Sorting a multidimensional array

Post by buchacho » Wed Jul 17, 2013 12:29 am

Update:

If I get rid of "numeric" it seems to work:

Code: Select all

sort fld "Field1"  by char 8 of each & char 9 of each &  char 10 of each &  char 11 of each & char 12 of each
But I would like to know if I can somehow restrict the sort to certain lines in the field. I want to preserve the formatting of the lines since some have a different color to differentiate new and old lines that have just been entered.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10331
Joined: Wed May 06, 2009 2:28 pm

Re: Sorting a multidimensional array

Post by dunbarx » Wed Jul 17, 2013 5:16 am

Hi.

Sort works on containers, so that if you only want to sort a portion of a container, you must first extract the lines in question, sort them, and then recombine, however you might effect that.

Craig

buchacho
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 50
Joined: Fri Jun 14, 2013 10:22 pm

Re: Sorting a multidimensional array

Post by buchacho » Wed Jul 17, 2013 5:13 pm

Is it possible for me to transfer the portions of the field I want to sort to a new field (and back again), while preserving the font color/format?

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Sorting a multidimensional array

Post by Klaus » Wed Jul 17, 2013 5:32 pm

Hi buchacho,

the trick to:
...
sort LINES OF fld "Field1" by char 8 of each & char 9 of each & char 10 of each & char 11 of each & char 12 of each
...
Your example IS in fact already sorted this way!
#################################
A C 1E012 01-A
A 1E012 1B-A
B 1E012 35-B
A 1E012 35-C
C 1E043 0X-B
A 1E212 36-A
ABC 1E234 2B-A
A 1E234 3B-A
C 1E234 35-A
#################################

And you can tranfer the HTML text of one field to another:
...
set the htmltext of fld "the other one..." to the htmltext of line 1 to 55 of fld "the original one..."
...


Best

Klaus

buchacho
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 50
Joined: Fri Jun 14, 2013 10:22 pm

Re: Sorting a multidimensional array

Post by buchacho » Thu Jul 18, 2013 6:26 pm

Your example IS in fact already sorted this way!
Might be, but when I test the script, I some lines around. =)
set the htmltext of fld "the other one..." to the htmltext of line 1 to 55 of fld "the original one..."
Helpful suggestion, thank you.

OK... One more thing on this topic. I would like to figure out how to properly use "numeric" in the script. My sorting is not putting numbers first, it is putting them mixed with alpha characters in their respective orders. How do I ensure it sorts "numeric" for each char?

Code: Select all

sort fld "Field1"  numeric by char 8 of each & char 9 of each &  char 10 of each &  char 11 of each & char 12 of each

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10331
Joined: Wed May 06, 2009 2:28 pm

Re: Sorting a multidimensional array

Post by dunbarx » Fri Jul 19, 2013 5:02 pm

I think you already answered your question. If you have, in fld 6:

a11a5
b33b8
c22c2

And this in a button.

Code: Select all

on mouseUp
   get fld "f6"
   sort it numeric by char 2 to 3 of each
   answer it
end mouseUp
The order of the chars in the lines is unchanged. The local variable "each" is evaluated according to the chunk expression you define, and the sort ignores everything else. So you can restrict the sort to any portion, even discontiguously (char 1 of each & char 5 of each).

Oh yes, is this what you were asking?

Craig

Post Reply