Answers.Body: <p>That SQL code snippet <a href="https://craftcms.stackexchange.com/a/2147/125">Brad posted</a> only works with the order parameter.
  The <code>group</code> filter doesn't allow SQL, but you could try to replicate the logic using Twig's <a href="http://twig.sensiolabs.org/doc/templates.html#other-operators" rel="nofollow noreferrer">ternary operator</a>.</p>
  
  <pre><code>{% set artists = craft.entries.section('lineup').order('(CASE WHEN field_sortTitle IS NULL OR field_sortTitle = "" THEN title ELSE field_sortTitle END)') %}
  {% set artistsGrouped = artists|group('{ object.sortTitle ? object.sortTitle[:1]|upper : object.title[:1]|upper }') %}
  
  {% for letter, artistsInGroup in artistsGrouped %}
      {{ letter }}
      {% for artist in artistsInGroup %}
          {{ artist.title }}
      {% endfor %}
  {% endfor %}
  </code></pre>
  
  <p>To shorten the expression a bit I used the <a href="http://twig.sensiolabs.org/doc/filters/slice.html" rel="nofollow noreferrer"><code>slice</code></a> filter's short syntax <code>[:1]</code> instead of <code>|slice(0, 1)</code>.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-03-14T17:21:28.620Z
Answers.Text: Thanks for the answer but that's not working for me I'm afraid. It outputs the lists of Artists but doesn't display the letter at the top of each. Doesn't seem to like the ternary operator in the group command.
Answers.UserDisplayName: Rob Webb
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-03-14T17:25:47.840Z
Answers.Text: This is what I'm aiming for:  B  The Beatles  Belle & Sebastian   Broken Bells   C   Caribou  The Cars  Chic
Answers.UserDisplayName: Rob Webb
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-03-14T19:44:59.507Z
Answers.Text: The shortcut syntax (i.e. `{title}`) to access the entry in the `group` filter appears to have problems with ternary operators. But it is also possible to access the entry via a variable `object` with normal Twig syntax (i.e. `{{ object.title }}`). You can then use any expression you like to, including ternary. The syntax within the filter looks a bit weird, because it already adds one pair of curly brackets for you (i.e. `title` and `{ object.title }`).
Answers.UserDisplayName: carlcs
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-03-14T14:14:51.217Z
Answers.LastActivityDate: 2015-03-14T19:23:09.350Z
Answers.LastEditDate: 2017-04-13T12:51:00.253Z
Answers.UserDisplayName: carlcs
Body: <p>Probably going to be an easy win for someone here, but being able to do this is currently eluding me. So:</p>
  
  <p>I'm trying to output a list of Artist names grouped by first letter but need to ignore 'The ' when it appears at the start of an Artist name. </p>
  
  <p>To do this, I've added a 'Sort Title' field to the Artist entry (as I figured there might be other occasions where this could be useful) - and using <a href="https://craftcms.stackexchange.com/questions/2146/from-a-template-how-can-you-order-entries-by-one-field-and-if-that-field-is-em/2147#2147">Brad's answer here</a> this works fine when outputting a standard alphabetical list.</p>
  
  <p>However, there's also a page that needs to display the Artists grouped by first letter. Code for that currently is:</p>
  
  <pre><code>{% for letter, artists in craft.entries.section('lineup').order('title') | group('title|upper|slice(0, 1)') %}
  </code></pre>
  
  <p>How best could I adapt that to use my sortTitle field (when it isn't empty) to group the Artist entries? I get 'Expected name or number' if I try to use Brad's code in place of 'title'.</p>
  
Tags: <templating><entries><sorting>
Title: Sorting entries A-Z, ignoring 'The' and grouping by first letter
ThreadText: # Sorting entries A-Z, ignoring 'The' and grouping by first letter
  
  (Asked by: username_0 on 2015-03-14)
  
  Probably going to be an easy win for someone here, but being able to do this is currently eluding me. So:
  
  I'm trying to output a list of Artist names grouped by first letter but need to ignore 'The ' when it appears at the start of an Artist name.
  
  To do this, I've added a 'Sort Title' field to the Artist entry (as I figured there might be other occasions where this could be useful) - and using [Brad's answer here](https://craftcms.stackexchange.com/questions/2146/from-a-template-how-can-you-order-entries-by-one-field-and-if-that-field-is-em/2147#2147) this works fine when outputting a standard alphabetical list.
  
  However, there's also a page that needs to display the Artists grouped by first letter. Code for that currently is:
  
  ```
  {% for letter, artists in craft.entries.section('lineup').order('title') | group('title|upper|slice(0, 1)') %}
  
  ```
  
  How best could I adapt that to use my sortTitle field (when it isn't empty) to group the Artist entries? I get 'Expected name or number' if I try to use Brad's code in place of 'title'.
  
  **Tags:** templating, entries, sorting
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2015-03-14. Score: 7 | Accepted answer
  
  That SQL code snippet [Brad posted](https://craftcms.stackexchange.com/a/2147/125) only works with the order parameter.
  The `group` filter doesn't allow SQL, but you could try to replicate the logic using Twig's [ternary operator](http://twig.sensiolabs.org/doc/templates.html#other-operators).
  
  ```
  {% set artists = craft.entries.section('lineup').order('(CASE WHEN field_sortTitle IS NULL OR field_sortTitle = "" THEN title ELSE field_sortTitle END)') %}
  {% set artistsGrouped = artists|group('{ object.sortTitle ? object.sortTitle[:1]|upper : object.title[:1]|upper }') %}
  
  {% for letter, artistsInGroup in artistsGrouped %}
      {{ letter }}
      {% for artist in artistsInGroup %}
          {{ artist.title }}
      {% endfor %}
  {% endfor %}
  
  ```
  
  To shorten the expression a bit I used the [`slice`](http://twig.sensiolabs.org/doc/filters/slice.html) filter's short syntax `[:1]` instead of `|slice(0, 1)`.
  
  > **Comments:**
  > **username_0**: Thanks for the answer but that's not working for me I'm afraid. It outputs the lists of Artists but doesn't display the letter at the top of each. Doesn't seem to like the ternary operator in the group command.  
  > **username_0**: This is what I'm aiming for:  B  The Beatles  Belle & Sebastian   Broken Bells   C   Caribou  The Cars  Chic  
  > **username_1**: The shortcut syntax (i.e. `{title}`) to access the entry in the `group` filter appears to have problems with ternary operators. But it is also possible to access the entry via a variable `object` with normal Twig syntax (i.e. `{{ object.title }}`). You can then use any expression you like to, including ternary. The syntax within the filter looks a bit weird, because it already adds one pair of curly brackets for you (i.e. `title` and `{ object.title }`).  
  
  ---
  
  