title: Linq OrderBy not sorting correctly 100% of the time
body: <p>I'm using the Linq <code>OrderBy()</code> function to sort a generic list of Sitecore items by display name, then build a string of pipe-delimited guids, which is then inserted into a Sitecore field. The display name is a model number of a product, generally around 10 digits. At first it seemed like this worked 100% of the time, but the client found a problem with it...</p>
  
  <p><a href="https://i.stack.imgur.com/eEPw4.png" rel="noreferrer"><img src="https://i.stack.imgur.com/eEPw4.png" alt="enter image description here"></a></p>
  
  <p>This is one example that we have found so far. The code somehow thinks <code>IC-30R-LH</code> comes after <code>IC-30RID-LH</code>, but the opposite should be true. </p>
  
  <p>I put this into an online alphabetizer like <a href="http://alphabetizer.flap.tv/" rel="noreferrer">this one</a> and it was able to get it right...</p>
  
  <p><a href="https://i.stack.imgur.com/OJbxY.png" rel="noreferrer"><img src="https://i.stack.imgur.com/OJbxY.png" alt="enter image description here"></a></p>
  
  <p>I did try adding <code>StringComparer.InvariantCultureIgnoreCase</code> as a second parameter to the <code>OrderBy()</code> but it did not help.</p>
  
  <p>Here's the code... Let me know if you have any ideas. Note that I am not running this <code>OrderBy()</code> call inside of a loop, at any scope.</p>
  
  <pre><code>    private string GetAlphabetizedGuidString(Item i, Field f)
      {
          List&lt;Item&gt; items = new List&lt;Item&gt;();
          StringBuilder scGuidBuilder = new StringBuilder();
  
          if (i != null &amp;&amp; f != null)
          {
              foreach (ID guid in ((MultilistField)f).TargetIDs)
              {
                  Item target = Sitecore.Data.Database.GetDatabase("master").Items.GetItem(guid);
                  if (target != null &amp;&amp; !string.IsNullOrEmpty(target.DisplayName)) items.Add(target);
              }
  
              // Sort it by item name.
              items = items.OrderBy(o =&gt; o.DisplayName, StringComparer.InvariantCultureIgnoreCase).ToList();
  
              // Build a string of pipe-delimited guids.
              foreach (Item item in items)
              {
                  scGuidBuilder.Append(item.ID);
                  scGuidBuilder.Append("|");
              }
  
              // Return string which is a list of guids.
              return scGuidBuilder.ToString().TrimEnd('|');
          }
  
          return string.Empty;
      }
  </code></pre>