Sorting a multidimensional array

Peter M. Brigham pmbrig at gmail.com
Fri Sep 26 11:55:16 EDT 2014


On Sep 26, 2014, at 12:24 AM, Peter Bogdanoff wrote:

> My first foray into arrays:
> 
> I want to sort a 2D array by one of the keys.
> 
> The data consists of several 2-line groups that are in a field. The lines contain styled and linked characters that I want to retain after sorting:
> 
> peaches
> pears
> 
> apples
> oranges
> 
> plums
> grapes
> 
> I want to sort the data by line 1 of each 2-line group, then display the sorted data as styled text.
> 
> I build an array:
> 
> repeat with x = 1 to (the number of lines of field “tField" / 3)
> 	put line 1 of field “tField" into tMultiArray[x][1]
> 	put the htmlText of line 1 of field “tField" into tMultiArray[x][2]
> 	put the htmlText of line 2 of field “tField" into tMultiArray[x][3]
> 	delete line 1 to 3 of field “tField"
> end repeat
> 
> How do I sort this array by line 1 of the plain text of each group?
> 
> Then, how do I extract the data from the array—I need the 2nd & 3rd values only (the htmlText) to display again in the field (set the htmlText of line x of field “tField” to…?)?
> 
> Is this the best method to sort this kind of data? The data comes as search results using Scott McDonald’s RunRev Planet Search tool, so I have to work with what the tool gives me.

You could do it without any arrays:

on sortIt tFldRef
   put the htmlText of tFldRef into tData
   put numtochar(4) into groupDiv
   put numtochar(5) into itemDiv
   replace "<p><p>" with groupDiv in tData
   replace"<p>" with itemDiv in tData
   replace groupDiv with cr in tData
   set the itemdel to itemDiv
   sort lines of tData by item 1 of each
   replace cr with "<p><p>" in tData
   replace itemDiv with "<p>" in tData
   set the htmltext of tFldRef to tData
end sortIt

Not tested, may need to do more parsing of HTMLtext before sorting…. But you get the idea.

-- Peter

Peter M. Brigham
pmbrig at gmail.com
http://home.comcast.net/~pmbrig

 



More information about the use-livecode mailing list