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!
"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.
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:
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, 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:
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....
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.
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.
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.
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..."
...
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?
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).