Answers.Body: <p>Custom searches inherit CRM_Contact_Form_Search_Custom_Base; check out the <code>addSortOffset()</code> function below (around line 177) to see how the sort is tacked onto the query:</p>
  
  <pre><code>public function addSortOffset(&amp;$sql, $offset, $rowcount, $sort) {
      if (!empty($sort)) {
        if (is_string($sort)) {
          $sort = CRM_Utils_Type::escape($sort, 'String');
          $sql .= " ORDER BY $sort ";
        }
        else {
          $sql .= " ORDER BY " . trim($sort-&gt;orderBy());
        }
      }
      if ($rowcount &gt; 0 &amp;&amp; $offset &gt;= 0) {
        $offset = CRM_Utils_Type::escape($offset, 'Int');
        $rowcount = CRM_Utils_Type::escape($rowcount, 'Int');
        $sql .= " LIMIT $offset, $rowcount ";
      }
    }
  </code></pre>
  
  <p>If you want to use an array it will send it to CRM_Utils_Sort::orderby(). I think you can set your $sort like this:</p>
  
  <pre><code>if (!$justIDs &amp;&amp;!sort) {
    $sort = array (
      'name' =&gt; 'civicrm_created_date',
      'direction' =&gt; 1,
    );
  }
  </code></pre>
  
  <p>Would that work for you?</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-05-18T13:40:32.497Z
Answers.LastActivityDate: 2015-05-18T14:04:47.900Z
Answers.LastEditDate: 2015-05-18T14:04:47.900Z
Answers.UserDisplayName: Nicholai
Body: <p>There is a function helper </p>
  
  <pre><code>function all($offset = 0, $rowcount = 0, $sort = NULL, $includeContactIDs = FALSE, $justIDs = FALSE) {
    // delegate to $this-&gt;sql(), $this-&gt;select(), $this-&gt;from(), $this-&gt;where(), etc.
    return $this-&gt;sql($this-&gt;select(), $offset, $rowcount, $sort, $includeContactIDs, NULL);
  }
  </code></pre>
  
  <p>But I'd like to set a sort order if not set. All the examples I found are writing directly their sql, as opposed to sql() that expect a CRM_Utils_Sort class (or that's what I understand based on the error messages)</p>
  
  <p>So what I'd like to do is to add a </p>
  
  <pre><code>if (!$justIDs &amp;&amp;!sort)
     //how to initialise sort so it ends up like ORDER BY created_date)?
  </code></pre>
  
Tags: <custom-search>
Title: How to provide a default sort on a custom search?
ThreadText: # How to provide a default sort on a custom search?
  
  (Asked by: username_0 on 2015-05-10)
  
  There is a function helper
  
  ```
  function all($offset = 0, $rowcount = 0, $sort = NULL, $includeContactIDs = FALSE, $justIDs = FALSE) {
    // delegate to $this->sql(), $this->select(), $this->from(), $this->where(), etc.
    return $this->sql($this->select(), $offset, $rowcount, $sort, $includeContactIDs, NULL);
  }
  
  ```
  
  But I'd like to set a sort order if not set. All the examples I found are writing directly their sql, as opposed to sql() that expect a CRM\_Utils\_Sort class (or that's what I understand based on the error messages)
  
  So what I'd like to do is to add a
  
  ```
  if (!$justIDs &&!sort)
     //how to initialise sort so it ends up like ORDER BY created_date)?
  
  ```
  
  **Tags:** custom-search
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2015-05-18. Score: 1
  
  Custom searches inherit CRM\_Contact\_Form\_Search\_Custom\_Base; check out the `addSortOffset()` function below (around line 177) to see how the sort is tacked onto the query:
  
  ```
  public function addSortOffset(&$sql, $offset, $rowcount, $sort) {
      if (!empty($sort)) {
        if (is_string($sort)) {
          $sort = CRM_Utils_Type::escape($sort, 'String');
          $sql .= " ORDER BY $sort ";
        }
        else {
          $sql .= " ORDER BY " . trim($sort->orderBy());
        }
      }
      if ($rowcount > 0 && $offset >= 0) {
        $offset = CRM_Utils_Type::escape($offset, 'Int');
        $rowcount = CRM_Utils_Type::escape($rowcount, 'Int');
        $sql .= " LIMIT $offset, $rowcount ";
      }
    }
  
  ```
  
  If you want to use an array it will send it to CRM\_Utils\_Sort::orderby(). I think you can set your $sort like this:
  
  ```
  if (!$justIDs &&!sort) {
    $sort = array (
      'name' => 'civicrm_created_date',
      'direction' => 1,
    );
  }
  
  ```
  
  Would that work for you?
  
  
  ---
  
  