Answers.Body: <p>Per your comment, I see you want to know about reversing the order of <em>multi-value custom fields</em>.  I don't think you can flip the return array directly.  But if you're consuming it in PHP/JS/etc., you can iterate through the loop backwards.  See <a href="https://stackoverflow.com/questions/10777597/reverse-order-of-foreach-list-items">https://stackoverflow.com/questions/10777597/reverse-order-of-foreach-list-items</a> for a PHP example.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-11-14T08:09:15.283Z
Answers.LastActivityDate: 2017-11-14T08:09:15.283Z
Answers.UserDisplayName: Jon G - Megaphone Tech
Answers.Body: <p>Yes, you can, though it's not well-documented!</p>
  
  <p>I recalled that there was some sort of "sort" option, which I confirmed by checked the <a href="https://wiki.civicrm.org/confluence/display/CRMDOC/Using+the+API" rel="nofollow noreferrer">Using the API</a> documentation.  However, the documentation is incomplete (I'll fix that in a minute).</p>
  
  <p>Fortunately, I found <a href="https://github.com/civicrm/civicrm-core/blob/master/api/v3/utils.php#L833" rel="nofollow noreferrer">the code that handles this</a>, and saw that you can sort by various field options.  See my screenshot below; I also confirmed that sorting by <code>last_name desc</code> also works.  You should be able to sort by any field.</p>
  
  <p><a href="https://i.sstatic.net/xwGde.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/xwGde.png" alt="enter image description here"></a> </p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-11-01T17:46:42.450Z
Answers.Text: This is great. Thanks for finding this information. Unfortunately for my case the sorting needs to be for the results of a field that has multiple values. I don't think I can sort those values. I have updated my original question with the results that I currently get.
Answers.UserDisplayName: Sleewok
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-11-01T15:24:38.050Z
Answers.LastActivityDate: 2016-11-01T15:24:38.050Z
Answers.UserDisplayName: Jon G - Megaphone Tech
Body: <p>I am using the CustomValues Get API I would like to have the most recent entries listed first.</p>
  
  <p>Is there a way that I can sort results in reverse when placing an API get? </p>
  
  <p>For example, below I want to sort the values['217'] so that values['217']['10'] is the first. Here are the results I get:</p>
  
  <pre><code>{
  "is_error": 0,
  "version": 3,
  "count": 1,
  "id": 217,
  "values": {
      "217": {
          "entity_id": "7088",
          "latest": "2016-11-01 00:00:00",
          "id": "217",
          "1": "2016-10-25 00:00:00",
          "2": "2016-10-18 00:00:00",
          "3": "2016-01-01 00:00:00",
          "4": "2016-01-01 00:00:00",
          "5": "2016-01-01 00:00:00",
          "6": "2016-10-27 00:00:00",
          "7": "2016-10-27 00:00:00",
          "8": "2016-10-27 00:00:00",
          "9": "2016-10-27 00:00:00",
          "10": "2016-10-27 00:00:00",
      }
  }
  }
  </code></pre>
  
  <p>Thanks!</p>
  
Tags: <api>
Title: Can API Return Reverse Results?
ThreadText: # Can API Return Reverse Results?
  
  (Asked by: username_0 on 2016-11-01)
  
  I am using the CustomValues Get API I would like to have the most recent entries listed first.
  
  Is there a way that I can sort results in reverse when placing an API get?
  
  For example, below I want to sort the values['217'] so that values['217']['10'] is the first. Here are the results I get:
  
  ```
  {
  "is_error": 0,
  "version": 3,
  "count": 1,
  "id": 217,
  "values": {
      "217": {
          "entity_id": "7088",
          "latest": "2016-11-01 00:00:00",
          "id": "217",
          "1": "2016-10-25 00:00:00",
          "2": "2016-10-18 00:00:00",
          "3": "2016-01-01 00:00:00",
          "4": "2016-01-01 00:00:00",
          "5": "2016-01-01 00:00:00",
          "6": "2016-10-27 00:00:00",
          "7": "2016-10-27 00:00:00",
          "8": "2016-10-27 00:00:00",
          "9": "2016-10-27 00:00:00",
          "10": "2016-10-27 00:00:00",
      }
  }
  }
  
  ```
  
  Thanks!
  
  **Tags:** api
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2016-11-01. Score: 3 | Accepted answer
  
  Yes, you can, though it's not well-documented!
  
  I recalled that there was some sort of "sort" option, which I confirmed by checked the [Using the API](https://wiki.civicrm.org/confluence/display/CRMDOC/Using+the+API) documentation. However, the documentation is incomplete (I'll fix that in a minute).
  
  Fortunately, I found [the code that handles this](https://github.com/civicrm/civicrm-core/blob/master/api/v3/utils.php#L833), and saw that you can sort by various field options. See my screenshot below; I also confirmed that sorting by `last_name desc` also works. You should be able to sort by any field.
  
  [![enter image description here](https://i.sstatic.net/xwGde.png)](https://i.sstatic.net/xwGde.png)
  
  > **Comments:**
  > **username_0**: This is great. Thanks for finding this information. Unfortunately for my case the sorting needs to be for the results of a field that has multiple values. I don't think I can sort those values. I have updated my original question with the results that I currently get.  
  
  ---
  
  ### Answer by username_1 on 2017-11-14. Score: 0
  
  Per your comment, I see you want to know about reversing the order of *multi-value custom fields*. I don't think you can flip the return array directly. But if you're consuming it in PHP/JS/etc., you can iterate through the loop backwards. See <https://stackoverflow.com/questions/10777597/reverse-order-of-foreach-list-items> for a PHP example.
  
  
  ---
  
  