Answers.Body: <p>I think my mistake was using the functions array_push and array_merge, as they assigned new keys to the array elements (activity types). Therefore, the single activity types couldn't be found in the following code anymore.</p>
  <p>I changed these two expressions to:</p>
  <pre><code>$new_array[$key] = $aTypes[$key]; /// instead of: array_push($new_array, $aTypes[$key]);
  </code></pre>
  <p>and</p>
  <pre><code>$new_array = $new_array + $aTypes; /// instead of: $new_array = array_merge($new_array, $aTypes);
  </code></pre>
  <p>With that, it seems to work now.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2020-10-27T11:05:25.320Z
Answers.LastActivityDate: 2020-10-27T11:05:25.320Z
Answers.UserDisplayName: emma
Body: <p>I want to sort the Activity Types that appear in the dropdown menue in CaseView, when clicking &quot;Add Activity&quot;. It should be sorted according to a determined order (based on frequency of use).</p>
  <p>Therefore, I built a custom sort function that I included in sites/all/modules/civicrm/CRM/Case/Form/CaseView.php:</p>
  <pre><code>  public function compareToSample($aTypes, $sample_array) {
      $new_array = array();
      $size_sample = count($sample_array);
      // search for first element of $samplearray in $atypes
      for ($newarrayindex = 0; $newarrayindex &lt; $size_sample; $newarrayindex++) {
        $key = array_search($sample_array[$newarrayindex], $aTypes, FALSE);
        if($key) {
          // found an element -&gt; save at the end of $new_array
          array_push($new_array, $aTypes[$key]);
          unset($aTypes[$key]); // delete it from $aTypes
        }
      }
      // if after the whole process $aTypes is not empty, add the rest of elements to end of $new_array
      if(!(empty($aTypes))) {
        if(empty($new_array)) {
          // no element in common
          return $aTypes;
        }
        else {
          $new_array = array_merge($new_array, $aTypes);
        }
      }
      return $new_array;
    }
  
  public function mySort($array_to_sort) {
      $sample_array = array(...some string elements representing the activity types in the right order...);
      if(empty($array_to_sort)) {
        return null; // EXCEPTION
      }
      $result = $this-&gt;compareToSample($array_to_sort, $sample_array);
      return $result;
    }
  </code></pre>
  <p>I am calling this function with <code>$aTypes = $this-&gt;mySort($aTypes);</code> inside the <code>buildQuickForm()</code> function in CaseView.php. (I am calling it just after the place where the original code is also sorting <code>$aTypes</code>, that is, just after:</p>
  <pre><code>if (!$xmlProcessor-&gt;getNaturalActivityTypeSort()) {
        asort($aTypes);
      }
  </code></pre>
  <p>The problem is now that the function only seems to sort the <em>labels</em> of the Activity Types, but not the Types themselves. So when clicking on one type, it will create an activity with another type of the list. How can I get the function to sort the real elements?
  Any hint is appreciated :)</p>
  
Tags: <civicase><activities>
Title: Sort function for Activities in CaseView
ThreadText: # Sort function for Activities in CaseView
  
  (Asked by: username_0 on 2020-10-27)
  
  I want to sort the Activity Types that appear in the dropdown menue in CaseView, when clicking "Add Activity". It should be sorted according to a determined order (based on frequency of use).
  Therefore, I built a custom sort function that I included in sites/all/modules/civicrm/CRM/Case/Form/CaseView.php:
    public function compareToSample($aTypes, $sample_array) {
      $new_array = array();
      $size_sample = count($sample_array);
      // search for first element of $samplearray in $atypes
      for ($newarrayindex = 0; $newarrayindex < $size_sample; $newarrayindex++) {
        $key = array_search($sample_array[$newarrayindex], $aTypes, FALSE);
        if($key) {
          // found an element -> save at the end of $new_array
          array_push($new_array, $aTypes[$key]);
          unset($aTypes[$key]); // delete it from $aTypes
        }
      }
      // if after the whole process $aTypes is not empty, add the rest of elements to end of $new_array
      if(!(empty($aTypes))) {
        if(empty($new_array)) {
          // no element in common
          return $aTypes;
        }
        else {
          $new_array = array_merge($new_array, $aTypes);
        }
      }
      return $new_array;
    }
  
  public function mySort($array_to_sort) {
      $sample_array = array(...some string elements representing the activity types in the right order...);
      if(empty($array_to_sort)) {
        return null; // EXCEPTION
      }
      $result = $this->compareToSample($array_to_sort, $sample_array);
      return $result;
    }
  
  I am calling this function with $aTypes = $this->mySort($aTypes); inside the buildQuickForm() function in CaseView.php. (I am calling it just after the place where the original code is also sorting $aTypes, that is, just after:
  if (!$xmlProcessor->getNaturalActivityTypeSort()) {
        asort($aTypes);
      }
  
  The problem is now that the function only seems to sort the labels of the Activity Types, but not the Types themselves. So when clicking on one type, it will create an activity with another type of the list. How can I get the function to sort the real elements?
  Any hint is appreciated :)
  
  **Tags:** civicase, activities
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2020-10-27. Score: 2 | Accepted answer
  
  I think my mistake was using the functions array_push and array_merge, as they assigned new keys to the array elements (activity types). Therefore, the single activity types couldn't be found in the following code anymore.
  I changed these two expressions to:
  $new_array[$key] = $aTypes[$key]; /// instead of: array_push($new_array, $aTypes[$key]);
  
  and
  $new_array = $new_array + $aTypes; /// instead of: $new_array = array_merge($new_array, $aTypes);
  
  With that, it seems to work now.
  
  
  ---
  
  