Sorting ACF Repeater Query by Sub Field in Bricks

This Pro tutorial shows how the rows of a ACF Repeater can be sorted alphabetically by the value of one of its sub fields when using Bricks.

Repeater being populated for a Page:

Before implementing the tutorial:

After:

Set up the field group as shown earlier.

Edit the singular template for your post type to which you have attached the field group with Bricks.

Copy and paste the Section’s JSON from here.

Add the following in child theme‘s functions.php or a code snippets plugin:

add_filter( 'bricks/query/run', function( $results, $query_obj ) {
	if ( $query_obj->element_id !== '40ff60' ) return $results;
  
	// sort the $results array alphabetically by the value of $result['document_title']
	usort( $results, function( $a, $b ) {
		return strcmp( $a['document_title'], $b['document_title'] );
	} );

	return $results;
}, 30, 2 );

Replace 40ff60 with the Bricks ID of the element for which query loop is enabled.