I currently have a List (searchResults) of strings which are all sentences, which contain the mostPopular string word (mostPopular) in a large piece of text (SentenceList). This all works very well, I'm able to count the number of occurrences each word has in each sentence in the second foreach loop, shown by the d++. However, I'm having trouble then ordering each sentence in searchResults by the most popular word shown by d.
List<string> searchResults = SentenceList.FindAll(s => s.Contains(mostPopular));
foreach (string i in searchResults)
{ int d = 0;
string[] T = i.Split(" ");
foreach (string l in T)
{
if (l.Contains(mostPopular)) { d++; }
else { continue; }
}
Console.WriteLine(i + d);
}
}
Any help would be greatly appreciated, or any recommendations on improving the question, to help me find an answer would be great!
My overall goal is to find the sentence which has the most occurrences of the most popular word, I need it in an ordered list because then I want to select a number of the strings depending on the value typed in by the user.
Many thanks