Sorting with Transforms

June 29th, 2016

Available in version 2.8. This functionality is an ALPHA version.
Transform introduction

The biggest complaint about the exiting “orderby” is that is does a lexical sort instead of a natural sort. I.e. it sort like: 1, 10, 11, 2, 20, 21 instead of 1, 2, 10, 20. Transforms offer natural sorting as well as your own custom sorts.

Built-in sorts

Natural Sort

To use a natural sort, use NaturalSortByField with the field name such as:

[cfdb-table form="myform" trans="NaturalSortByField(last_name)"]

Sort Ascending or Descending:

[cfdb-table form="myform" trans="NaturalSortByField(last_name,ASC)"]

[cfdb-table form="myform" trans="NaturalSortByField(last_name,DESC)"]

If you have other transforms, generally you would have the sorting transform last:

[cfdb-table form="myform" trans="last_name=ucwords(last_name)&&NaturalSortByField(last_name)"]

Lexical Sort

If you want a lexical sort, use SortByField:

[cfdb-table form="myform" trans="SortByField(last_name)"]

Multi-Field Sort

If you want to sort on more than one field (up to 3) (no ASC/DESC on this)

[cfdb-table form="myform" trans="NaturalSortByMultiField(last_name,first_name)"]

or use SortByMultiField instead above.

Or you can chain sorts together, as many as you like:

[cfdb-table form="myform" trans="NaturalSortByField(city)&&NaturalSortByField(street)&&NaturalSortByField(number)"]

Date Sort

Have a field with date information like “5/25/2014” and want to sort. Use SortByDateField.

[cfdb-table form="myform" trans="SortByDateField(date)"]

For ascending or descending use “ASC” or “DESC”

[cfdb-table form="myform" trans="SortByDateField(date,DESC)"]

To specify the date format to ensure it is interpreted correctly, add the format:

[cfdb-table form="myform" trans="SortByDateField(date,ASC,m/d/Y)"]

See date formats.

Custom Sorts

You may wish to build your own sorting function. Do it like this:

[cfdb-table form="myform" trans="MyCustomSort"]

 

Enter code via Shortcodes Actions and Filters:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
require_once(ABSPATH . 'wp-content/plugins/contact-form-7-to-database-extension/SortTransform.php');
 
class MyCustomSort extends SortTransform {
 
    /**
     * @param $a array: associative array of 1 form entry
     * @param $b array: associative array of 1 form entry
     * @return -1 if a>b, 0 if a==b, 1 if a<b
     */
    public function sort($a, $b) {
       // Compare values in $a and $b
       // For example, to compare a "name" field,
       //   compare $a['name'] and $b['name']
       //
       // return -1 if $a comes first
       // return 1 if $b comes first
       // return 0 if it is a tie
       return 0;
    }
}

There is a more abbreviated to add your own custom sort on a field. You just provide the comparison function that return -1, 0, or 1.

[cfdb-table form="myform" trans="SortByFunctionAndField(my_compare,score)"]

Enter code via Shortcodes Actions and Filters:

1
2
3
4
5
6
7
8
9
require_once(ABSPATH . 'wp-content/plugins/contact-form-7-to-database-extension/CFDBPermittedFunctions.php');
 
function my_compare($a, $b) {
if ($a > $b) return -1;
if ($a < $b) return 1;
return 0;
}
 
cfdb_register_function('my_compare');

 

Comments are closed.  Go To Support Forum