---
title: "Filter Sort Group Array Data"
description: "Generate documents that display array data filtered by criteria, sorted in order, and grouped by specific fields for clear, organised presentation."
canonical_url: "https://resources.docmosis.com/template-tutorials/filter-sort-group-repeating-content"
last_reviewed: 2026-09-02
---

# Filter Sort Group Array Data

Filtering, sorting, and grouping directives help select and control how data appears in the generated document.
These directives can only be used with the 

- <<rs_ [repeating sections](/template-tutorials/repeating-sections)
- <<rr_ [repeating rows](/template-tutorials/tables)

### **Filtering**

The :filter()directive allows data to be shown only when a criteria is met.
The following template syntax:
<<rs_products:filter(type='Fruit')>>
<<name>>
<<es_products:filter(type='Fruit')>> 
 is the same as:
FOR all **products** WHERE **type** is **Fruit**LOOP
OUTPUT **name**
END LOOP

### **Sorting**

The :sort()directive allows data to be displayed in ascending or descending order.
The following template syntax:
<<rs_products:sort(name)>>
<<name>>
<<es_products:sort(name)>>
is the same as:
FOR all **products** SORTED by **name**LOOP
OUTPUT **name**
END LOOP 

> **Note**: By default the data is sorted in ascending order .To display the names in descending order, use:<<rs_products:sort(DESC,name)>>

### **Grouping**

The :group()directive allows data to be grouped by a specific field or expression.
As soon as a :group() directive is encountered, the following two temporary variables are automatically created:

1. $groupKey : holds the value of the field or expression used for grouping
2. $groupItems : collects all items that share the group key value.

The Docmosis engine loops through each distinct $groupKey value and repopulates $groupItems for each new $groupkey, creating groups.

> **Note**: $groupkey and $groupitems are available only within the repeating section or row where the grouping occurs.

The following template syntax:
<<rs_products:group(type)>>
<<$groupKey>>
<<rs_$groupItems>>;
<<name>>
<<es_$groupItems>>
<<es_products:group(type)>>
is the same as:
FOR all **products** GROUPED by **type** LOOP
OUTPUT **type**
FOR all **products** of that **type** LOOP
OUTPUT **name**
END LOOP
END LOOP

## Filter Sort Group Array Data - example template syntax

**Display All Products**

<<rs_ products>>
<<name>> is a <<type>>
<<es_ products>>

**Filtering**

<<rs_products:filter(type='Fruit')>>
<<name>> is a <<type>>
<<es_products:filter(type='Fruit')>>


**Sorting**

<<rs_products:sort(name)>>
<<name>> is a <<type>>
<<es_products:sort(name)>>

**Grouping**

<<rs_products:group(type)>>
<<$groupKey>>
<<rs_$groupItems>>
<<name>>
<<es_$groupItems>>
<<es_products:group(type)>>