title: C# sorting list by int, but by another int if duplicated values are found
body: <p>I'm working with a C# list with containing a custom class. The class looks like this;</p>
  
  <pre><code>    public class GeneratorLine
      {
          public string TextString { get; set; }
          public int DatabaseId { get; set; }
          public int LineNumber { get; set; }
          public int Rank { get; set; }
      }
  </code></pre>
  
  <p>Now, I want to print the "TextString" to a .txt-file, and the linenumber should be the main controller for where the lines are placed in the file. However, duplicated values of linenumbers can occur in which case they should be grouped by "DatabaseId" and placed in the file in this order. If a value is present in "Rank", they should be ordered by this.</p>
  
  <p>I have tried with different constallations of GroupBy, OrderBy etc., but my main problem is that the same "DatabaseId" can have low "Linenumbers" and high "Linenumbers", and a different "DatabaseId" can have "Linenumbers" in between those.</p>
  
  <pre><code>Example of file output:
  Text1 (DatabaseId: 1, Linenumber: 1, Rank: null)
  Text2 (DatabaseId: 1, Linenumber: 2, Rank: null)
  Text3 (DatabaseId: 1, Linenumber: 3, Rank: null)
  Text4 (DatabaseId: 3, Linenumber: 4, Rank: 1)
  Text5 (DatabaseId: 3, Linenumber: 5, Rank: 1)
  Text6 (DatabaseId: 3, Linenumber: 6, Rank: 1)
  Text7 (DatabaseId: 2, Linenumber: 4, Rank: 2)
  Text8 (DatabaseId: 2, Linenumber: 5, Rank: 2)
  Text9 (DatabaseId: 2, Linenumber: 6, Rank: 2)
  Text10 (DatabaseId: 1, Linenumber: 7, Rank: null)
  </code></pre>
  
  <p>Any help is much appreciated.</p>