title: Sort by Count Occurrences of a Word in list rows linq
body: <p>How do I sort a list by the occurrence of word in every row of linq data?  I got an answer from here by someone that is giving the right output.  Here is the code:</p>
  
  <pre><code>void Main()
  {
      List&lt;SearchResult&gt; list = new List&lt;SearchResult&gt;() { 
          new SearchResult(){ID=1,Title="Geo Prism GEO 1995 GEO* - ABS #16213899"},
          new SearchResult(){ID=2,Title="Excavator JCB - ECU P/N: 728/35700"},
          new SearchResult(){ID=3,Title="Geo Prism GEO 1995 - ABS #16213899"},
          new SearchResult(){ID=4,Title="JCB Excavator JCB- ECU P/N: 728/35700"},
          new SearchResult(){ID=5,Title="Geo Prism GEO,GEO 1995 - ABS #16213899 GEO"},
          new SearchResult(){ID=6,Title="dog"},
      };
  
      var to_search = new[] { "Geo", "JCB" };
  
      var result = from searchResult in list
           let key_string = to_search.FirstOrDefault(ts =&gt;  searchResult.Title.ToLower().Contains(ts.ToLower()))
           group searchResult by key_string into Group
           orderby Group.Count() descending
           select Group;
           result.ToList().Dump();
  
  
  
   }
  // Define other methods and classes here
  public class SearchResult
  {
      public int ID { get; set; }
      public string Title { get; set; }
  }
  </code></pre>
  
  <p>I am getting the output like</p>
  
  <pre><code>ID Title 
  -- ------
  1  Geo Prism GEO 1995 GEO* - ABS #16213899 
  3  Geo Prism GEO 1995 - ABS #16213899 
  5  Geo Prism GEO,GEO 1995 - ABS #16213899 GEO 
  2  Excavator JCB - ECU P/N: 728/35700 
  4  JCB Excavator JCB- ECU P/N: 728/35700 
  6  dog 
  </code></pre>
  
  <p>the above output is ok.  All rows having the ord GEO comes first because it found maximum time in most of the rows means GEO the word found in 3 rows and JCB found in two rows so JCB related rows comes next.</p>
  
  <p>I need another sort after getting the above output on whole data.  That is that GEO rows comes first which row has the GEO word maximum time.  So my output would look like below:</p>
  
  <pre><code>ID Title 
  -- ------
  5  Geo Prism GEO,GEO 1995 - ABS #16213899 GEO 
  1  Geo Prism GEO 1995 GEO* - ABS #16213899 
  3  Geo Prism GEO 1995 - ABS #16213899 
  4  JCB Excavator JCB- ECU P/N: 728/35700 
  2  Excavator JCB - ECU P/N: 728/35700 
  6  dog 
  </code></pre>
  
  <p>I found a linq query which counts occurrences of a word in string:</p>
  
  <pre><code>string text = @"Historically, the world of data and data the world of objects data" ;
  string searchTerm = "data";
  //Convert the string into an array of words
  string[] source = text.Split(new char[] { '.', '?', '!', ' ', ';', ':', ',' },   StringSplitOptions.RemoveEmptyEntries);
  var matchQuery = from word in source
               where word.ToLowerInvariant() == searchTerm.ToLowerInvariant()
               select word;
  int wordCount = matchQuery.Count();
  </code></pre>
  
  <p>I got it from <a href="http://msdn.microsoft.com/en-us/library/bb546166.aspx" rel="nofollow noreferrer">this url</a></p>
  
  <p>How could I use the above code to sort my title?  How could use second sort for counting the occurrences of a word in title field as a result my output would look like:</p>
  
  <pre><code>ID Title 
  -- ------
  5  Geo Prism GEO,GEO 1995 - ABS #16213899 GEO 
  1  Geo Prism GEO 1995 GEO* - ABS #16213899 
  3  Geo Prism GEO 1995 - ABS #16213899 
  4  JCB Excavator JCB- ECU P/N: 728/35700 
  2  Excavator JCB - ECU P/N: 728/35700 
  6  dog 
  </code></pre>