KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Utility method to compare two collections
PRODUCT: 4D | VERSION: 18 | PLATFORM: Mac & Win
Published On: August 24, 2020

Below is a useful method for comparing two collections. The method will return a boolean, stating whether the first collection contains the same data as the second collection.

// ----------------------------------------------------
// Method: compareCollection
// Description: Compares the contents of 2 collections and returns True if they contain the same data; returns False otherwise.
// Parameters: $1 & $2 - Collections to compare
// NOTE: Order matters (i.e. ["1";"2"] is not equal to ["2";"1"])
// ----------------------------------------------------

C_BOOLEAN($0)
C_COLLECTION($1;$2)

If (Count parameters>=2)
  
   C_TEXT($container_t)
   C_OBJECT($container_o)
  
   $container_o:=New object("content";$1)
   $container_t:=JSON Stringify($container_o)
   $digest1_t:=Generate digest($container_t;4D digest)
  
   $container_o:=New object("content";$2)
   $container_t:=JSON Stringify($container_o)
   $digest2_t:=Generate digest($container_t;4D digest)
  
   $0:=($digest1_t=$digest2_t)
  
End if

For example, say that we have the 2 collections below:

$a:=New collection("hello";"world";1;2;3)
$b:=New collection("hello";"world";1;2;3)

Passing the collections into compareCollection will return True. Note that order matters, so if the collections were the following instead, the method would return False:

$a:=New collection("world";"hello";1;2;3)
$b:=New collection("hello";"world";1;2;3)