Sorting a 2D Array
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
Sorting a 2D Array
I have a two-dimensional array, raceTimes. The times (integers) will be in the third position of each "row", if you like. I'm trying to find a simple method to sort the values that are stored at position 3, lowest at the first index of the array.
I tried looking around the LiveCode lesson library, but couldn't really find much.
Thanks
I tried looking around the LiveCode lesson library, but couldn't really find much.
Thanks
-
- VIP Livecode Opensource Backer
- Posts: 9648
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Sorting a 2D Array
Hi.
Ordinary data processing tasks (like sorting) are done in "normal" variables, not array variables. I call that "in the clear". Arrays are associated pairs of data, and do not lend themselves to being crunched. You can sort the keys, but little else.
So especially if you are sorting by the third whatever (item?) in each line, you have to (pseudo):
You may want to think about doing other stuff while still an ordinary variable, and whether or not you really need to restore to an array.
Craig Newman
Ordinary data processing tasks (like sorting) are done in "normal" variables, not array variables. I call that "in the clear". Arrays are associated pairs of data, and do not lend themselves to being crunched. You can sort the keys, but little else.
So especially if you are sorting by the third whatever (item?) in each line, you have to (pseudo):
Code: Select all
combine yourArrayVariable with appropriate delimiters --return and whatever
sort lines of the now ordinary variable by item 3 of each -- again, if items is what you are thinking
split back into an array variable --if that is what you need
Craig Newman
-
- VIP Livecode Opensource Backer
- Posts: 9823
- Joined: Sat Apr 08, 2006 7:05 am
- Location: Los Angeles
- Contact:
Re: Sorting a 2D Array
Associative arrays are hashed indices of pointers, and have no sense of order among their elements. Whether or not the input going into an array was sorted, once in an array a collection of elements will no longer have inherent order.
To solve the problem at hand, get the keys of the array and sort those, and then use those to access elements in the order you need them, e.g.:
To solve the problem at hand, get the keys of the array and sort those, and then use those to access elements in the order you need them, e.g.:
Code: Select all
-- Make an array:
split tSomeData by cr and tab
-- Get the keys:
put the keys of tSomeData into tKeys
-- Sort 'em:
sort lines of tKeys
-- Do whatever's needed with each element in the sorted order:
repeat for each line tKey in tKeys
DoSomethingWith tSomeData[tKey]
end repeat
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
-
- VIP Livecode Opensource Backer
- Posts: 9648
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Sorting a 2D Array
@ smith8867
Ah.
I missed the two-dimensional part. In that case using the "combine" command loses interior dimensions.
@ Richard.
What do you think about an enhancement to the "combine" command, where any number of delimiters might be applied? In that way, one could directly deconstruct a multi-dimensional array as simply as a one-dimensional on. So if you had:
You would end up with an ordinary variable:
Actually, I would like to be able to use the same delimiter as often as I like:
to yield:
Craig
Ah.
I missed the two-dimensional part. In that case using the "combine" command loses interior dimensions.
@ Richard.
What do you think about an enhancement to the "combine" command, where any number of delimiters might be applied? In that way, one could directly deconstruct a multi-dimensional array as simply as a one-dimensional on. So if you had:
Code: Select all
on mouseUp
put 10 into myArray["a"]["d"]
put 20 into myArray["b"]["e"]
put 30 into myArray["c"]["f"]
combine myArray with return and comma and "*"
end mouseUp
Code: Select all
a,d*10
b,e*20
c,f*30
Code: Select all
combine myArray with return and comma and comma
Code: Select all
a,d,10
b,e,20
c,f,30
-
- VIP Livecode Opensource Backer
- Posts: 2262
- Joined: Thu Feb 28, 2013 11:52 pm
- Location: Göttingen, DE
Re: Sorting a 2D Array
Hi all.
The term "2D-array" is not unique, so it may be misleading here.
Some people use the term for an array whose ELEMENTS are 2D, some use the term for an array whose KEYS are 2D. For example the dictionary uses for his matrix handlers the following:
"A two-dimensional array is an array whose elements have a two-part key to describe them. You can visualize such an array as a set of rows and columns:
the first part of each element's key is the row number, and the second part is the column number.
For example, the expression myArray[3,2] describes the element of myArray which is in the third row, second column."
If you don't use this second type for your array then you should think about using it, because you can then also apply LC's fast matrix operations for such arrays.
Moreover you could use LC 8.1 or later because you can then filter elements and keys of the array what may save a lot of time and typing:
For example.
If your array is rTimes[i,j] with i indicating the rows and j indicating the columns then this line extracts a subarray consisting out of the third column (i.e. the third element in each row)
filter keys of rTimes with "*,3" into rTCol3 -- third column
Now rTCol3 is the subarray rTimes[i,3] with i=1 to num of rows (third column).
Accordingly this would take out the subarray which is the 4th row
filter keys of rTimes with "4,*" into rTRow2 -- fourth row
Now this takes out the third column, sorts its values ascending numerically while retaining the row and column indices and displays the result as (row index, column index, value)
Note the advantage of that: Your original array rTimes isn't touched.
The term "2D-array" is not unique, so it may be misleading here.
Some people use the term for an array whose ELEMENTS are 2D, some use the term for an array whose KEYS are 2D. For example the dictionary uses for his matrix handlers the following:
"A two-dimensional array is an array whose elements have a two-part key to describe them. You can visualize such an array as a set of rows and columns:
the first part of each element's key is the row number, and the second part is the column number.
For example, the expression myArray[3,2] describes the element of myArray which is in the third row, second column."
If you don't use this second type for your array then you should think about using it, because you can then also apply LC's fast matrix operations for such arrays.
Moreover you could use LC 8.1 or later because you can then filter elements and keys of the array what may save a lot of time and typing:
For example.
If your array is rTimes[i,j] with i indicating the rows and j indicating the columns then this line extracts a subarray consisting out of the third column (i.e. the third element in each row)
filter keys of rTimes with "*,3" into rTCol3 -- third column
Now rTCol3 is the subarray rTimes[i,3] with i=1 to num of rows (third column).
Accordingly this would take out the subarray which is the 4th row
filter keys of rTimes with "4,*" into rTRow2 -- fourth row
Now this takes out the third column, sorts its values ascending numerically while retaining the row and column indices and displays the result as (row index, column index, value)
Code: Select all
filter keys of rTimes with "*,3" into rTCol3 -- third column
combine rTCol3 with cr and comma
sort rTCol3 ascending numeric by item 3 of each
put rTCol3 into fld "OUT"
shiftLock happens
-
- VIP Livecode Opensource Backer
- Posts: 7229
- Joined: Sat Apr 08, 2006 8:31 pm
- Location: Minneapolis MN
- Contact:
Re: Sorting a 2D Array
As -hh said, we need to know what type of array you mean by "2D". Is the array a list of keys where each key has a single value? Or is it a list of keys where each key contains a second array?smith8867 wrote:I have a two-dimensional array, raceTimes. The times (integers) will be in the third position of each "row", if you like. I'm trying to find a simple method to sort the values that are stored at position 3, lowest at the first index of the array.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com
HyperActive Software | http://www.hyperactivesw.com
-
- VIP Livecode Opensource Backer
- Posts: 9648
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Sorting a 2D Array
I think the OP meant the latter, that a second array is associated with each key:
So Jacque, what about the enhancement? It would bring arrays much, er, clearer much more simply.
Craig
Third position. That is why I backpedaled.The times (integers) will be in the third position of each "row", i
So Jacque, what about the enhancement? It would bring arrays much, er, clearer much more simply.
Craig
Re: Sorting a 2D Array
The 2-D array I am using is the row column type, like a table it is the elements one.
What I need to do, is sort the values in the third "column" for each row, and write the sorted "table", so each row and column in the array, to a text file.
This stuffs sort of new to me: my understanding from the replies above is that if I take the third "column" value of the array and put them into separate variables, then the original data is not sorted along with it. ie. the other data in the row.
So, I *think* I need to check the 3rd values between two rows, changing the index value for the row, sort of like a swap and sort it from there? Again, sort of new to this sorting arrays business, so excuse my clunky explanations - I hope I'm making a bit of sense.
What I need to do, is sort the values in the third "column" for each row, and write the sorted "table", so each row and column in the array, to a text file.
This stuffs sort of new to me: my understanding from the replies above is that if I take the third "column" value of the array and put them into separate variables, then the original data is not sorted along with it. ie. the other data in the row.
So, I *think* I need to check the 3rd values between two rows, changing the index value for the row, sort of like a swap and sort it from there? Again, sort of new to this sorting arrays business, so excuse my clunky explanations - I hope I'm making a bit of sense.
-
- VIP Livecode Opensource Backer
- Posts: 9648
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Sorting a 2D Array
Aha.
You do not have an array, in LC parlance. You have a table. An array is a very specific type of variable, and we can talk about them some other time.
A table is invariably constructed similar to an excel spreadsheet, that is, rows are delimited by returns, and fields are delimited by tabs. The itemDelimiter is your friend here.
If indeed you are set up that way, simply:
If the delimiters are different, simply substitute. The default sort is by lines, so this need not be explicitly mentioned (sort lines of yourData by...)
Is your data in a LC table field?
Craig
You do not have an array, in LC parlance. You have a table. An array is a very specific type of variable, and we can talk about them some other time.
A table is invariably constructed similar to an excel spreadsheet, that is, rows are delimited by returns, and fields are delimited by tabs. The itemDelimiter is your friend here.
If indeed you are set up that way, simply:
Code: Select all
set the itemDel to tab
sort yourData by item 3 of each
Is your data in a LC table field?
Craig
Re: Sorting a 2D Array
That makes sense Craig. No its being read it in from an external CSV file. The above table is just an example of the data being read in.dunbarx wrote:Aha.
You do not have an array, in LC parlance. You have a table. An array is a very specific type of variable, and we can talk about them some other time.
A table is invariably constructed similar to an excel spreadsheet, that is, rows are delimited by returns, and fields are delimited by tabs. The itemDelimiter is your friend here.
If indeed you are set up that way, simply:If the delimiters are different, simply substitute. The default sort is by lines, so this need not be explicitly mentioned (sort lines of yourData by...)Code: Select all
set the itemDel to tab sort yourData by item 3 of each
Is your data in a LC table field?
Craig
So that should work once I've read the data in?
-
- VIP Livecode Opensource Backer
- Posts: 9648
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Sorting a 2D Array
Hi.
Yes.
CSV file uses commas to delimit fields, and returns to delimit records. CSV is dangerous, since commas are all too common within the text of a single field in an unstructured dataset. But conceptually, there is no issue. Just leave the itemDel to its default, comma, and sort by the third item of each line as per the above code snippet.
Craig
Yes.
CSV file uses commas to delimit fields, and returns to delimit records. CSV is dangerous, since commas are all too common within the text of a single field in an unstructured dataset. But conceptually, there is no issue. Just leave the itemDel to its default, comma, and sort by the third item of each line as per the above code snippet.
Craig
Re: Sorting a 2D Array
I wonder how hard it would be to set a delimiter as a null character for conversion or, alternately, if you saved it out in a different format (null delimited instead of comma/tab/space/etc). After all, a null character isn't something you would probably ever type on a keyboard while entering data.dunbarx wrote:CSV is dangerous, since commas are all too common within the text of a single field in an unstructured dataset.