Sorting a 2D 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
smith8867
Posts: 57
Joined: Tue Nov 18, 2014 5:36 pm

Sorting a 2D Array

Post by smith8867 » Mon Sep 18, 2017 7:49 pm

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

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 a 2D Array

Post by dunbarx » Mon Sep 18, 2017 10:49 pm

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):

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

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9823
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Sorting a 2D Array

Post by FourthWorld » Tue Sep 19, 2017 12:23 am

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.:

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

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 a 2D Array

Post by dunbarx » Tue Sep 19, 2017 4:21 am

@ 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:

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
You would end up with an ordinary variable:

Code: Select all

a,d*10
b,e*20
c,f*30
Actually, I would like to be able to use the same delimiter as often as I like:

Code: Select all

combine myArray with return and comma and comma
to yield:

Code: Select all

a,d,10
b,e,20
c,f,30
Craig

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Sorting a 2D Array

Post by [-hh] » Tue Sep 19, 2017 5:35 am

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)

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"
Note the advantage of that: Your original array rTimes isn't touched.
shiftLock happens

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 a 2D Array

Post by jacque » Tue Sep 19, 2017 8:31 pm

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.
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?
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

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 a 2D Array

Post by dunbarx » Tue Sep 19, 2017 11:56 pm

I think the OP meant the latter, that a second array is associated with each key:
The times (integers) will be in the third position of each "row", i
Third position. That is why I backpedaled.

So Jacque, what about the enhancement? It would bring arrays much, er, clearer much more simply.

Craig

smith8867
Posts: 57
Joined: Tue Nov 18, 2014 5:36 pm

Re: Sorting a 2D Array

Post by smith8867 » Wed Sep 20, 2017 1:41 pm

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.

smith8867
Posts: 57
Joined: Tue Nov 18, 2014 5:36 pm

Re: Sorting a 2D Array

Post by smith8867 » Wed Sep 20, 2017 1:47 pm

Just to clarify, what I'm sorting is something like this.
Image

I need to sort the integers in column three whilst retaining the user1, user 2 etc. So that, once its sorted, I can write/ display the array data somewhere.

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 a 2D Array

Post by dunbarx » Wed Sep 20, 2017 2:45 pm

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:

Code: Select all

set the itemDel to tab
sort yourData by item 3 of each
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

smith8867
Posts: 57
Joined: Tue Nov 18, 2014 5:36 pm

Re: Sorting a 2D Array

Post by smith8867 » Thu Sep 21, 2017 5:35 pm

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:

Code: Select all

set the itemDel to tab
sort yourData by item 3 of each
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
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.

So that should work once I've read the data in?

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 a 2D Array

Post by dunbarx » Thu Sep 21, 2017 7:00 pm

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

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

Re: Sorting a 2D Array

Post by bogs » Fri Sep 22, 2017 6:55 pm

dunbarx wrote:CSV is dangerous, since commas are all too common within the text of a single field in an unstructured dataset.
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.
Image

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”