[TIP} co-sort variable 1 based on values in variable 2

Jim Ault JimAultWins at yahoo.com
Wed Mar 21 00:52:10 EDT 2007


On 3/20/07 7:47 PM, "J. Landman Gay" <jacque at hyperactivesw.com> wrote:

> Jim Ault wrote:
>>  The basic idea is to sort varB by the values in varA using a temporary
>> array.
> Do you need to use an array? Would this do?:
>         sort items of varB by itemOffset(each,varA)

No.
The reason for the array is that we need to reorder the varA items by
sorting (descending) --> thus varB gets reordered accordingly.
 
In your example, the itemoffset(each,varA) will = 0 unless there is an item
in varA that matches one in varB.

Dick's version says (step-by-step)
itemoffset(each,varB) -- the position of the item in its own variable
varA[1] --return the item in varA that is in the same position
now sort by the returned values from varA

thus
  put "car,bike,VWvan,glider" into varA
  put "key,helmet,rubberband,hang" into of varB

split varA using cr and comma:
[1] car
[2] bike
[3] VWVan
[4] glider

then Rev builds the lists
itemoffset("key",varB) => 1 => varA[1] => car
itemoffset("helmet",varB) => 2 => varA[2] => bike
itemoffset("rubberband",varB) => 3 => varA[3] => VWVan
itemoffset("hang",varB) => 4 => varA[4] => glider

now Rev sorts by the returned values, descending text...
bike, car, glider,VWVan  <-- after sorting
thus 
helmet, key,hang, rubberband  --is the correct order

(a rubberband can be used as a fan belt in the VW)
the others make sense :-)
------
My version would build the varA array the long way
My version would not take advantage of using "each"
My version worked, but it ain't as good as Dick's, not even close.


Jim Ault
Las Vegas






More information about the use-livecode mailing list