Answers.Body: <p>I don't know if it's possible (but would like to see a query solution).</p>
  
  <p>There is one problem with your setup: If an entry gets a "substantial update", you can't make a correction afterwards, since it would become the latest item again.</p>
  
  <p>I propose you write a plugin which would:</p>
  
  <ul>
  <li>Wait for the <a href="http://buildwithcraft.com/docs/plugins/events-reference#entries-onBeforeSaveEntry" rel="nofollow">'onBeforeSaveEntry' event</a></li>
  <li>Check if the <a href="http://buildwithcraft.com/docs/lightswitch-fields" rel="nofollow">Lightswitch-field</a> is "on"</li>
  <li>If true, set <a href="http://buildwithcraft.com/docs/templating/entrymodel#postDate" rel="nofollow">postDate</a> to the current date</li>
  <li>Set the Lightswitch-field to "off" again</li>
  </ul>
  
  <p>.</p>
  
  <p><strong>Quick-and-Dirty example:</strong></p>
  
  <pre class="lang-php prettyprint-override"><code>public function init()
  {
      parent::init();
  
      //Event: onBeforeSaveEntry
      craft()-&gt;entries-&gt;onBeforeSaveEntry = function(Event $event) {
  
          //Check if its the right section
          if ( $event-&gt;entry-&gt;section == 'news' ) {
  
              //Check if the lightswitch-field is "on"
              if ( $event-&gt;entry-&gt;lightswitch_field_handle == 1 ) {
                  //Set postDate to current date
                  $event-&gt;entry-&gt;postDate = $event-&gt;entry-&gt;dateUpdated;
  
                  //Set Lightswitch-filed to "off"
                  $event-&gt;entry-&gt;lightswitch_field_handle = 0;
              }
  
          }
  
          //Return
          return $event;
  
      };
  }
  </code></pre>
  
  <p>If you add this function to your <a href="http://buildwithcraft.com/docs/plugins/setting-things-up#your-primary-plugin-class" rel="nofollow">primary plugin class</a> and sort your entries by <code>postDate</code> it should work just like you want.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-08T13:33:01.623Z
Answers.Text: This is quite an interesting solution, wondering if we could produce a solution based around this idea. Thank you :)
Answers.UserDisplayName: Ian Nebbiolo
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-08T13:52:16.013Z
Answers.Text: Forgot to say that you just need to add this function to your primary plugin class and sort your entries by postDate.
Answers.UserDisplayName: Victor
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-08T14:27:54.927Z
Answers.Text: If you're going to the trouble of resetting the postDate programatically, why don't you just ask your client to change the postDate themselves—they'd be changing the lightswitch field anyway? This would give you the exact same result but without any extra code.
Answers.UserDisplayName: Stuart Whitehead
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-08T14:29:53.467Z
Answers.Text: @StuartWhitehead I really don't know why I haven't thought of that -.-
Answers.UserDisplayName: Victor
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-09T16:56:35.567Z
Answers.Text: @VictorIn Your staggering SE rep is clouding your judgment, Victor. :)
Answers.UserDisplayName: Matt Stein
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-10T06:54:39.183Z
Answers.Text: @StuartWhitehead VictorIn Thanks guys. We did consider that as a solution too ;)
Answers.UserDisplayName: Ian Nebbiolo
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-08T12:31:19.293Z
Answers.LastActivityDate: 2014-07-08T13:51:13.797Z
Answers.LastEditDate: 2014-07-08T13:51:13.797Z
Answers.UserDisplayName: Victor
Answers.Body: <p>My <a href="http://michaelrog.com/craft/lagniappe/supersort" rel="nofollow">SuperSort</a> plugin is built for use cases exactly such as this. It allows you to render each object in your array using a Twig object template and sort the array based on the rendered value.</p>
  
  <p>So, you could do something like:</p>
  
  <pre><code>{% set sortedEntries = entries | supersort('sortAs', '{{ object.myLightswitch == 'y' ? object.dateUpdated: object.postDate }}') %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-01-24T00:32:34.453Z
Answers.LastActivityDate: 2015-01-24T00:32:34.453Z
Answers.UserDisplayName: Michael Rog
Answers.Body: <p>I think I'd create a <a href="http://twig.sensiolabs.org/doc/advanced.html#functions" rel="nofollow">twig function</a> or filter to create a custom sort (<a href="http://www.php.net/manual/en/function.usort.php" rel="nofollow">usort</a> in php), pass the collection to the function so it's returned 'sorted'.</p>
  
  <p>Would be interested in seeing if anyone has a nice craft.entries query straight off the bat for this though.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-08T12:05:31.110Z
Answers.LastActivityDate: 2014-07-08T12:30:17.740Z
Answers.LastEditDate: 2014-07-08T12:30:17.740Z
Answers.UserDisplayName: billythekid
Answers.Body: <p>You can do this entirely from the <a href="http://buildwithcraft.com/docs/templating/craft.entries#order"><code>order</code></a> param using MySQL’s <a href="http://dev.mysql.com/doc/refman/5.0/en/case.html"><code>CASE</code></a> syntax:</p>
  
  <pre><code>{% set newsItems = craft.entries({
      section: 'news',
      order:   '(CASE field_myLightswitchField
                    WHEN 1 THEN elements.dateUpdated
                    ELSE elements.dateCreated
                END) DESC'
  }) %}
  </code></pre>
  
  <p>Note that you’ll need to include that “field_” prefix before your Lightswitch field’s handle since this is a more complex condition and Craft won’t be sure if it should add that automatically.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-07-08T14:45:21.063Z
Answers.LastActivityDate: 2014-07-08T14:45:21.063Z
Answers.UserDisplayName: Brandon Kelly
Body: <p>I'm trying to work out how I can order news posts by postDate, unless a lightswitch is toggled for a post, at which point it will use the dateUpdated to order that item.</p>
  
  <p>This is so a client can still change posts without them appearing at the top of the list (for corrections, etc), but also make substantial updates to certain posts and have them appear as the latest newest item.</p>
  
Tags: <entries><ordering><postdate>
Title: No ordinary news post ordering
ThreadText: # No ordinary news post ordering
  
  (Asked by: username_0 on 2014-07-08)
  
  I'm trying to work out how I can order news posts by postDate, unless a lightswitch is toggled for a post, at which point it will use the dateUpdated to order that item.
  
  This is so a client can still change posts without them appearing at the top of the list (for corrections, etc), but also make substantial updates to certain posts and have them appear as the latest newest item.
  
  **Tags:** entries, ordering, postdate
  
  
  ---
  
  ## 4 answers
  
  ### Answer by username_6 on 2014-07-08. Score: 8 | Accepted answer
  
  You can do this entirely from the [`order`](http://buildwithcraft.com/docs/templating/craft.entries#order) param using MySQL’s [`CASE`](http://dev.mysql.com/doc/refman/5.0/en/case.html) syntax:
  
  ```
  {% set newsItems = craft.entries({
      section: 'news',
      order:   '(CASE field_myLightswitchField
                    WHEN 1 THEN elements.dateUpdated
                    ELSE elements.dateCreated
                END) DESC'
  }) %}
  
  ```
  
  Note that you’ll need to include that “field\_” prefix before your Lightswitch field’s handle since this is a more complex condition and Craft won’t be sure if it should add that automatically.
  
  
  ---
  
  ### Answer by username_1 on 2014-07-08. Score: 4
  
  I don't know if it's possible (but would like to see a query solution).
  
  There is one problem with your setup: If an entry gets a "substantial update", you can't make a correction afterwards, since it would become the latest item again.
  
  I propose you write a plugin which would:
  
  * Wait for the ['onBeforeSaveEntry' event](http://buildwithcraft.com/docs/plugins/events-reference#entries-onBeforeSaveEntry)
  * Check if the [Lightswitch-field](http://buildwithcraft.com/docs/lightswitch-fields) is "on"
  * If true, set [postDate](http://buildwithcraft.com/docs/templating/entrymodel#postDate) to the current date
  * Set the Lightswitch-field to "off" again
  
  .
  
  **Quick-and-Dirty example:**
  
  ```php
  public function init()
  {
      parent::init();
  
      //Event: onBeforeSaveEntry
      craft()->entries->onBeforeSaveEntry = function(Event $event) {
  
          //Check if its the right section
          if ( $event->entry->section == 'news' ) {
  
              //Check if the lightswitch-field is "on"
              if ( $event->entry->lightswitch_field_handle == 1 ) {
                  //Set postDate to current date
                  $event->entry->postDate = $event->entry->dateUpdated;
  
                  //Set Lightswitch-filed to "off"
                  $event->entry->lightswitch_field_handle = 0;
              }
  
          }
  
          //Return
          return $event;
  
      };
  }
  
  ```
  
  If you add this function to your [primary plugin class](http://buildwithcraft.com/docs/plugins/setting-things-up#your-primary-plugin-class) and sort your entries by `postDate` it should work just like you want.
  
  > **Comments:**
  > **username_0**: This is quite an interesting solution, wondering if we could produce a solution based around this idea. Thank you :)  
  > **username_1**: Forgot to say that you just need to add this function to your primary plugin class and sort your entries by postDate.  
  > **username_2**: If you're going to the trouble of resetting the postDate programatically, why don't you just ask your client to change the postDate themselves—they'd be changing the lightswitch field anyway? This would give you the exact same result but without any extra code.  
  > **username_1**: @username_2 I really don't know why I haven't thought of that -.-  
  > **username_3**: @VictorIn Your staggering SE rep is clouding your judgment, username_1. :)  
  > **username_0**: @username_2 VictorIn Thanks guys. We did consider that as a solution too ;)  
  
  ---
  
  ### Answer by username_4 on 2015-01-24. Score: 3
  
  My [SuperSort](http://username_4.com/craft/lagniappe/supersort) plugin is built for use cases exactly such as this. It allows you to render each object in your array using a Twig object template and sort the array based on the rendered value.
  
  So, you could do something like:
  
  ```
  {% set sortedEntries = entries | supersort('sortAs', '{{ object.myLightswitch == 'y' ? object.dateUpdated: object.postDate }}') %}
  
  ```
  
  
  ---
  
  ### Answer by username_5 on 2014-07-08. Score: 3
  
  I think I'd create a [twig function](http://twig.sensiolabs.org/doc/advanced.html#functions) or filter to create a custom sort ([usort](http://www.php.net/manual/en/function.usort.php) in php), pass the collection to the function so it's returned 'sorted'.
  
  Would be interested in seeing if anyone has a nice craft.entries query straight off the bat for this though.
  
  
  ---
  
  