Sorting 2D Arrays using Multi Sort Array in V11
Date: 08-02-10
From: "George F. Huhn" <GHuhn_at_Datamachines.com>
In Versions 2003 * 2004, one could sort 2-dimensional arrays by row with
single-dimensional arrays by using "Multi Sort Array." This undocumented
capability is no longer available in V11 and throws an error message.
For those that need this ability, the following code provides a workaround.
First, create an array of the same size as the arrays you will be sorting,
and populate each element with the corresponding element number:
C_LONGINT($Counter_l;$Size_of_Array_l)
$Size_of_Array_l:=Size of array(Sort_Array_ar)
ARRAY LONGINT(Order_By_Array_al;$Size_of_Array_l)
For ($Counter_l;1;$Size_of_Array_l)
Idle
Order_By_Array_al {$Counter_l}:=$Counter_l
End for
Next, use this array in place of the 2D array in the Multi Sort Array. This
array will then capture the final sorted element order.
MULTI SORT ARRAY(Sort_Array_ar;<;Array1_ar;Array2_ar; Order_By_Array_al)
Next, create a new 2D array of the identical size of the one that you want
to sort. You are going to use the values in the "Order_By_Array_al" as a key
to map the unsortered 2D array ("Main_2D Array_al") into a sorted version
("$Sorted_Array_al"). This is accomplished by using "Copy Array" in a loop
to copy each row from the unsorted array into the new correctly ordered
array.
C_LONGINT($Number_of_Rows_l;$Number_of_Columns_l)
$Number_of_Rows_l:= $Size_of_Array_l
$Number_of_Columns_l:=Size of array(Main_2D Array_al{1})
ARRAY LONGINT($Sorted_Array_al;$Number_of_Rows_l;$Number_of_Columns_l)
C_LONGINT($Row_Counter_l;$Sorted_ Number_l)
$Row_Counter_l:=1
Repeat
Idle
$Sorted_Row_Number_l:=Order_By_Array_al {$Row_Counter_l}
COPY ARRAY(Main_2D Array_al
{$Sorted_Row_Number_l};$Sorted_Array_al{$Row_Counter_l})
$Row_Counter_l:=$Row_Counter_l+1
Until ($Row_Counter_l>=$Final_Size_of_Array_l)
Then just copy the sorted 2D array to the array that you originally wanted
sorted:
COPY ARRAY($Sorting_Array_al; Main_2D Array_al)
--
George F. Huhn, President
Data Machines, Inc.
Blog: http://blog.datamachines.com
Follow us at http://twitter.com/DataMachinesCom
**********************************************************************
The New 4D Partner Program - All the tools you need to be a successful 4D Developer.
Sign up Now: http://www.4d.com/community/partner/registration.html
4D Internet Users Group (4D iNUG)
FAQ: http://lists.4d.com/faqnug.html
Archive: http://lists.4d.com/archives.html
Options: https://lists.4d.com/mailman/options/4d_tech
Unsub: mailto:4D_Tech-Unsubscribe_at_lists.4D.com
**********************************************************************