Tech Tip: Determine two Collections' indexes that contain similar data
PRODUCT: 4D | VERSION: 17 | PLATFORM: Mac & Win
Published On: September 27, 2018
A Collection can contain different data types which is organized in an index fashion. Thus, it is possible to compare a Collection's index to another Collection using ".equal". The following utility method can report two Collections that have similar data from their index:
// ----------------------------------------------------------------- // Name: COL_INDEXES_OF_SIMILAR_DATA // Description: Method will compare 2 collections' content in each // index to report the indexes of similar data through an arrays. // // Input Parameters: // $1 (COLLECTION) - Collection #1 to compare // $2 (COLLECTION) - Collection #2 to compare // $3 (POINTER) - Pointer to array that will retrieve Col #1 index // $4 (POINTER) - Pointer to array that will retrieve Col #2 index // ------------------------------------------------------------------ C_COLLECTION($1;$col1;$tempCol1) C_COLLECTION($2;$col2;$tempCol2) C_POINTER($3;$arrIndexCol1) C_POINTER($4;$arrIndexCol2) C_LONGINT($i;$j) If (Count parameters=4) $col1:=$1 $col2:=$2 $arrIndexCol1:=$3 $arrIndexCol2:=$4 $tempCol1:=New collection $tempCol2:=New collection For ($i;0;$col1.length-1) $tempCol1[0]:=$col1[$i] For ($j;0;$col2.length-1) $tempCol2[0]:=$col2[$j] If ($tempCol1.equal($tempCol2)) APPEND TO ARRAY($arrIndexCol1->;$i) // Retrieve the index pos Col #1 APPEND TO ARRAY($arrIndexCol2->;$j) // Retrieve the index pos Col #2 End if End for End for End If |
Here is an example of two Collections:
C_COLLECTION(col1;col2) ARRAY LONGINT(arr1;0) ARRAY LONGINT(arr2;0) col1:=New collection(143;New object("name";"Bob";"age";32);2.34;1) col2:=New collection(New object("name";"Bob";"age";32);"Books";2.34;4) COL_INDEXES_OF_SIMILAR_DATA (col1;col2;->arr1;->arr2) |
Here is the result of the arrays that found similar content:
![](https://kb.4d.com:443/kbase/assets/images/78126-1.jpg)
See Also: