Dumb sort question

J. Landman Gay jacque at hyperactivesw.com
Wed Dec 4 15:15:01 EST 2002


On 12/4/02 12:26 PM, jbv wrote:

> But how can I sort a variable featuring 4 lines with several
> hundreds of items in each, so that (for instance) items of
> line 4 are sorted numeric ascending, and (that's important)
> items of other lines being moved according to the new
> position of each item of line 4 ?
> 
> For instance :
>     line 1 :    b,d,c,a
>     line 2 :    2,4,3,1
> 
> after sorting line 2, the content would become :
>     line 1 :    a,b,c,d
>     line 2 :    1,2,3,4
> 
> I have the feeling this can't be done in MC, while I remember
> doing it in OMO using the spreadsheet control...

You can do pretty much anything in MC. In this case, you can use a 
custom function to sort the data.

> P.S. I need to do it FAST on large variables, therefore a repeat
> loop won't do.

The following works for me with very short lists. It does have a repeat 
loop in it, so you'd have to see if it is fast enough for your needs. It 
could probably be optimized better.

local theOldOrder,theNewOrder

on dosort
   put fld 1 into theData
   put line 1 of theData into theOldOrder -- or whatever is the key line
   put theOldOrder into theNewOrder
   sort items of theNewOrder ascending numeric
   put theNewOrder & return into theNewData
   delete line 1 of theData
   repeat for each line l in theData
     sort items of l by mySort(each,l,theOldOrder,theNewOrder)
     put l & return after theNewData
   end repeat
   put theNewData into fld 2
end dosort

function mySort theItem,theLine
   set wholematches to true
   put itemoffset(theItem,theLine) into theItemNum
   put item theItemNum of theOldOrder into theOriginalItem
   put itemoffset(theOriginalItem,theNewOrder) into theNewPos
   return theNewPos
end mySort

-- 
Jacqueline Landman Gay         |     jacque at hyperactivesw.com
HyperActive Software           |     http://www.hyperactivesw.com




More information about the metacard mailing list