4D Blog

Home Product Sync Several Collections with this new Function

Sync Several Collections with this new Function

October 18, 2023

Product

4D offers powerful features to work with object collections. For example, you can create a collection of cities with their associated countries and continents. Manipulating these collections with built-in functions like sort, order, and filter becomes a breeze.

4D v20 R3 introduces the new multiSort() function to simplify this process. With this function, you can now easily sort and synchronize multiple collections.

New function

This new multiSort() function lets you carry out a multi-level synchronized sort on a set of collections.

If the function is called with no parameters, the function has the same effect as the .sort() function. The collection is sorted (only scalar values) in ascending order by default, according to their type.

If you want to sort the collections in some other order than ascending, you must supply a formula.

$col.multiSort( [ $col2 ; $col3 ] )
$col.multiSort( $formula; [$col2 ; $col3] )

It is also possible to define a multi-level synchronized sort. This is similar to the SORT MULTIPLE ARRAY command.

In this case, you need to pass an object containing collection and order attributes.

$col.multiSort( [ {collection: $col2; order: ck ascending} ; $col3 ] )
$col.multiSort( [ $formula; {collection: $col2; order: ck ascending} ; $col3 ] )

Moreover, the sorting algorithm is said to be stable, i.e., two elements with the same value appear in the same order in the sorted output as in the input data set.

Database example

Examples

Let’s go back to our example of city, country, and continent:

var $city : Collection
var $country : Collection
var $continent : Collection

$city:=["Paris"; "Lyon"; "Rabat"; "Eching"; "San Diego"]
$country:=["France"; "France"; "Morocco"; "Germany"; "US"]
$continent:=["Europe"; "Europe"; "Africa"; "Europe"; "America"]

Example 1: A simple sort of the first collection ($continent) with synchronization of the other collections ($country and $city)

$continent.multiSort([$country; $city])

The result is:

"Africa"; "America"; "Europe"; "Europe"; "Europe" 
"Morocco"; "US"; "France"; "France"; "Germany" 
"Rabat"; "San Diego"; "Paris"; "Lyon"; "Eching"

Example 2: Ascending sort of the first and the third collections ($continent and $city), and synchronization for the second collection ($country)

$continent.multiSort([$country; {collection: $city; order: ck ascending}])

The result is:

"Africa"; "America"; "Europe"; "Europe"; "Europe"
"Morocco"; "US"; "Germany"; "France"; "France"
"Rabat"; "San Diego"; "Eching"; "Lyon"; "Paris"

Example 3: Formula sort of the first collection ($continent) and synchronization for the other collections ($country and $city)

The formula in the example is straightforward, allowing you to sort in descending order. But you probably have plenty of ideas for more complex formulas to suit your needs.

$continent.multiSort(Formula($1.value>$1.value2); [$country; $city])

The result is:

"Europe"; "Europe"; "Europe"; "America"; "Africa"
"France"; "France"; "Germany"; "US"; "Morocco"
"Paris"; "Lyon"; "Eching"; "San Diego"; "Rabat"

Example 4: With complex collections

You can also use it to synchronize collections of objects.


var $name : Collection
var $address : Collection
$name:=[]
$name.push({firstname: "John"; lastname: "Smith"})
$name.push({firstname: "Alain"; lastname: "Martin"})
$name.push({firstname: "Jane"; lastname: "Doe"})
$name.push({firstname: "John"; lastname: "Doe"})
$address:=[]
$address.push({city: "Paris"; country: "France"})
$address.push({city: "Lyon"; country: "France"})
$address.push({city: "Eching"; country: "Germany"})
$address.push({city: "Berlin"; country: "Germany"})

$name.multiSort(Formula($1.value.firstname<$1.value2.firstname); [$address])

The result is:

"Alain Martin"; "Jane Doe"; "John Smith"; "John Doe"
"Lyon France"; "Eching Germany"; "Paris France"; "Berlin Germany" 

Next…

For more information, please refer to the documentation. You can share your feedback and use cases on the forum.

Discuss

Tags 21, Collections, Programming, v20 R3

Latest related posts

  • January 22, 2026

    Transform Static Documents into Actionable Knowledge with AIKit

  • January 22, 2026

    Deploy Fluent UI effortlessly in your 4D applications

  • January 21, 2026

    Searching Across Host Projects and Components in 4D

Vanessa Talbot
Vanessa Talbot
• Product Owner •Vanessa Talbot joined 4D Program team in June, 2014. As a Product Owner, she is in charge of writing the user stories then translating it to functional specifications. Her role is also to make sure that the feature implementation delivered is meeting the customer need.Since her arrival, she has worked to define key features in 4D. She has worked on most of preemptive multi-threading new features and also on a very complex subject: the new architecture for engined application. Vanessa has a degree from Telecom Saint-Etienne. She began her career at the Criminal Research Institute as a developer for the audiovisual department. She has also worked in media and medical fields as expert in technical support, production as well as documenting new features.
  • Deutsch
  • Français
  • English
  • Português
  • Čeština
  • Español
  • Italiano
  • 日本語

Categories

Browse categories

  • AI
  • 4D View Pro
  • 4D Write Pro
  • 4D for Mobile
  • Email
  • Development Mode
  • 4D Language
  • ORDA
  • User Interface / GUI
  • Qodly Studio
  • Server
  • Maintenance
  • Deployment
  • 4D Tutorials
  • Generic
  • 4D Summit sessions and other online videos

Tags

4D AIKit 4D for Android 4D for iOS 4D NetKit 4D Qodly Pro 4D View Pro 4D Write Pro 20 R10 21 Administration AI Artificial Intelligence Build application CI/CD Class Client/Server Code editor Collections Formula Google Listbox Logs Mail Microsoft 365 Network Objects OpenAI ORDA PDF Pictures Preemptive Programming REST Scalability Security Session Source control Speed Spreadsheet Tutorial UI User Experience vscode Web Word processor

Tags

4D AIKit 4D for Android 4D for iOS 4D NetKit 4D Qodly Pro 4D View Pro 4D Write Pro 20 R10 21 Administration AI Artificial Intelligence Build application CI/CD Class Client/Server Code editor Collections Formula Google Listbox Logs Mail Microsoft 365 Network Objects OpenAI ORDA PDF Pictures Preemptive Programming REST Scalability Security Session Source control Speed Spreadsheet Tutorial UI User Experience vscode Web Word processor
Subscribe to 4D Newsletter

© 2026 4D SAS - All rights reserved
Terms & Conditions | Legal Notices | Data Policy | Cookie Policy | Contact us | Write for us


Subscribe to 4D Newsletter

* Your privacy is very important to us. Please click here to view our Policy

Contact us

Got a question, suggestion or just want to get in touch with the 4D bloggers? Drop us a line!

* Your privacy is very important to us. Please click here to view our Policy