{"id":1135,"date":"2014-07-02T01:20:51","date_gmt":"2014-07-02T05:20:51","guid":{"rendered":"http:\/\/cfdbplugin.com\/?page_id=1135"},"modified":"2016-06-29T15:32:20","modified_gmt":"2016-06-29T19:32:20","slug":"sorting-with-transforms","status":"publish","type":"page","link":"https:\/\/cfdbplugin.com\/?page_id=1135","title":{"rendered":"Sorting with Transforms"},"content":{"rendered":"<p>Available in version 2.8. This functionality is an ALPHA version.<br \/>\n<a href=\"http:\/\/cfdbplugin.com\/?page_id=1118\">Transform introduction<\/a><\/p>\n<p>The biggest complaint about the exiting &#8220;orderby&#8221; 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.<\/p>\n<h2>Built-in sorts<\/h2>\n<h3>Natural Sort<\/h3>\n<p>To use a natural sort, use\u00a0NaturalSortByField with the field name such as:<\/p>\n<p><code>[cfdb-table form=\"myform\" <span style=\"color: #0000ff;\">trans=\"NaturalSortByField(last_name)<\/span>\"]<\/code><\/p>\n<p>Sort Ascending or Descending:<\/p>\n<p><code>[cfdb-table form=\"myform\"\u00a0<span style=\"color: #0000ff;\">trans=\"NaturalSortByField(last_name,ASC)<\/span>\"]<\/code><\/p>\n<p><code>[cfdb-table form=\"myform\"\u00a0<span style=\"color: #0000ff;\">trans=\"NaturalSortByField(last_name,DESC)<\/span>\"]<\/code><\/p>\n<p>If you have other transforms, generally you would have the sorting transform last:<\/p>\n<p><code>[cfdb-table form=\"myform\"\u00a0<span style=\"color: #0000ff;\">trans=\"last_name=ucwords(last_name)&amp;&amp;NaturalSortByField(last_name)<\/span>\"]<\/code><\/p>\n<h3>Lexical Sort<\/h3>\n<p>If you want a lexical sort, use SortByField:<\/p>\n<p><code>[cfdb-table form=\"myform\"\u00a0<span style=\"color: #0000ff;\">trans=\"SortByField(last_name)<\/span>\"]<\/code><\/p>\n<h3>Multi-Field Sort<\/h3>\n<p>If you want to sort\u00a0on more than one field (up to 3) (no ASC\/DESC on this)<\/p>\n<p><code>[cfdb-table form=\"myform\"\u00a0<span style=\"color: #0000ff;\">trans=\"NaturalSortByMultiField(last_name,first_name)<\/span>\"]<\/code><\/p>\n<p>or use SortByMultiField instead above.<\/p>\n<p>Or you can chain sorts together, as many as you like:<\/p>\n<p><code>[cfdb-table form=\"myform\"\u00a0<span style=\"color: #0000ff;\">trans=\"NaturalSortByField(city)&amp;&amp;NaturalSortByField(street)&amp;&amp;NaturalSortByField(number)<\/span>\"]<\/code><\/p>\n<h3>Date Sort<\/h3>\n<p>Have a field with date information like &#8220;5\/25\/2014&#8221; and want to sort. Use\u00a0SortByDateField.<\/p>\n<p><code>[cfdb-table form=\"myform\"\u00a0<span style=\"color: #0000ff;\">trans=\"SortByDateField(date)<\/span>\"]<\/code><\/p>\n<p>For ascending or descending use &#8220;ASC&#8221; or &#8220;DESC&#8221;<\/p>\n<p><code>[cfdb-table form=\"myform\"\u00a0<span style=\"color: #0000ff;\">trans=\"SortByDateField(date,DESC)<\/span>\"]<\/code><\/p>\n<p>To specify the date format to ensure it is interpreted correctly, add the format:<\/p>\n<p><code>[cfdb-table form=\"myform\"\u00a0<span style=\"color: #0000ff;\">trans=\"SortByDateField(date,ASC,m\/d\/Y)<\/span>\"]<\/code><\/p>\n<p>See <a href=\"http:\/\/www.php.net\/manual\/en\/datetime.createfromformat.php\" target=\"_blank\">date formats<\/a>.<\/p>\n<h2>Custom Sorts<\/h2>\n<p>You may wish to build your own sorting function. Do it like this:<\/p>\n<p><code>[cfdb-table form=\"myform\"\u00a0<span style=\"color: #0000ff;\">trans=\"MyCustomSort<\/span>\"]<\/code><\/p>\n<p>&nbsp;<\/p>\n<p>Enter code via <a href=\"http:\/\/wordpress.org\/plugins\/add-actions-and-filters\/\">Shortcodes Actions and Filters<\/a>:<\/p>\n<pre lang=\"php\" line=\"1\" escaped=\"true\">require_once(ABSPATH . 'wp-content\/plugins\/contact-form-7-to-database-extension\/SortTransform.php');\r\n\r\nclass MyCustomSort extends SortTransform {\r\n\r\n    \/**\r\n     * @param $a array: associative array of 1 form entry\r\n     * @param $b array: associative array of 1 form entry\r\n     * @return -1 if a&gt;b, 0 if a==b, 1 if a&lt;b\r\n     *\/\r\n    public function sort($a, $b) {\r\n       \/\/ Compare values in $a and $b\r\n       \/\/ For example, to compare a \"name\" field,\r\n       \/\/   compare $a['name'] and $b['name']\r\n       \/\/\r\n       \/\/ return -1 if $a comes first\r\n       \/\/ return 1 if $b comes first\r\n       \/\/ return 0 if it is a tie\r\n       return 0;\r\n    }\r\n} \r\n<\/pre>\n<p>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.<\/p>\n<p><code>[cfdb-table form=\"myform\"\u00a0<span style=\"color: #0000ff;\">trans=\"SortByFunctionAndField(my_compare,score)<\/span>\"]<\/code><\/p>\n<p>Enter code via\u00a0<a href=\"http:\/\/wordpress.org\/plugins\/add-actions-and-filters\/\">Shortcodes Actions and Filters<\/a>:<\/p>\n<pre lang=\"php\" line=\"1\" escaped=\"true\">require_once(ABSPATH . 'wp-content\/plugins\/contact-form-7-to-database-extension\/CFDBPermittedFunctions.php');\r\n\r\nfunction my_compare($a, $b) {\r\nif ($a &gt; $b) return -1;\r\nif ($a &lt; $b) return 1;\r\nreturn 0;\r\n}\r\n\r\ncfdb_register_function('my_compare');<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Available in version 2.8. This functionality is an ALPHA version. Transform introduction The biggest complaint about the exiting &#8220;orderby&#8221; 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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":1118,"menu_order":6,"comment_status":"closed","ping_status":"closed","template":"page-without-sidebar.php","meta":{"jetpack_post_was_ever_published":false,"footnotes":""},"class_list":["post-1135","page","type-page","status-publish","hentry"],"jetpack_shortlink":"https:\/\/wp.me\/P1mptf-ij","jetpack_sharing_enabled":true,"jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/pages\/1135","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1135"}],"version-history":[{"count":19,"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/pages\/1135\/revisions"}],"predecessor-version":[{"id":1510,"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/pages\/1135\/revisions\/1510"}],"up":[{"embeddable":true,"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=\/wp\/v2\/pages\/1118"}],"wp:attachment":[{"href":"https:\/\/cfdbplugin.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1135"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}