title: Reorder Log Files Custom Sort
body: <p>I am trying to solve a sorting algorithm .</p>
  <p>The problem is this .
  <em>You are given an array of logs. Each log is a space-delimited string of words, where the first word is the identifier.
  There are two types of logs:
  Letter-logs: All words (except the identifier) consist of lowercase English letters.
  Digit-logs: All words (except the identifier) consist of digits.
  eorder these logs so that:
  The letter-logs come before all digit-logs.
  The letter-logs are sorted lexicographically by their contents. If their contents are the same, then sort them lexicographically by their identifiers.
  The digit-logs maintain their relative ordering.</em></p>
  <pre><code>Input : { &quot;dig1 8 1 5 1&quot;, &quot;let1 art can&quot;, &quot;dig2 3 6&quot;, &quot;let2 own kit dig&quot;, &quot;let3 art zero&quot; };
  Output : { &quot;let1 art can&quot;,&quot;let3 art zero&quot;,  &quot;let2 own kit dig&quot;, &quot;dig1 8 1 5 1&quot;, &quot;dig2 3 6&quot; };
  </code></pre>
  <p>I have tried implementing using IComparer Inteface .</p>
  <pre><code>class Program
      {
          static void Main(string[] args)
          {
              string[] logs = new string[] { &quot;dig1 8 1 5 1&quot;, &quot;let1 art can&quot;, &quot;dig2 3 6&quot;, &quot;let2 own kit dig&quot;, &quot;let3 art zero&quot; };
              TagComparer tagComparer = new TagComparer();
               Array.Sort(logs, tagComparer);
              Console.WriteLine(&quot;Hello World!&quot;);
          }
      }
  
      public class TagComparer : IComparer&lt;string&gt;
      {
          public int Compare(string second, string first)
          {
              bool isFirstDigit = false;
              bool isSecondDigit = false;
              char firstCharOfFirstString = first.Split(&quot; &quot;)[1][0];
              char firstCharOfSecondString = second.Split(&quot; &quot;)[1][0];
              isFirstDigit = Regex.IsMatch(firstCharOfFirstString.ToString(), @&quot;\d&quot;);
              isSecondDigit = Regex.IsMatch(firstCharOfSecondString.ToString(), @&quot;\d&quot;);
  
              if (isFirstDigit &amp;&amp; isSecondDigit)
                  return 0;
              if(isFirstDigit &amp;&amp; isSecondDigit == false)
              {
                  return -1;
              }
              if (isFirstDigit == false &amp;&amp; isSecondDigit)
              {
                  return 1;
              }
              if(isFirstDigit == false &amp;&amp; isSecondDigit == false)
              {
                  string firstString = first.Split(&quot; &quot;)[1];
                  string secondString = second.Split(&quot; &quot;)[1];
                  string firstKey = first.Split(&quot; &quot;)[0];
                  string secondKey = second.Split(&quot; &quot;)[0];
                  int result = string.Compare(firstString, secondString);
                  if(result != 0)
                  {
                      return result;
                  }
                  else
                  {
                      return string.Compare(firstKey, secondKey);
                  }
              }
              return 0;
          }
  }
  </code></pre>
  <p>I am getting the output as :
  { &quot;let2 own kit dig&quot;, &quot;let1 art can&quot;,&quot;let3 art zero&quot;, &quot;dig1 8 1 5 1&quot;, &quot;dig2 3 6&quot; };</p>
  <p>There are two things which I am not able to understand</p>
  <ol>
  <li><p>When I debug and see the line</p>
  <p>public int Compare(string second, string first)
  first argument is : &quot;let1 art can&quot; and second argument is &quot;let2 own kit dig&quot;. So I had to inevitably interchange arguments as <code>Compare(string second, string first)</code> instead of <code>Compare(string first, string second)</code></p>
  </li>
  <li><p>How can I make changes to algorithm to sort correctly.</p>
  </li>
  </ol>