I have this method:
public static IEnumerable<string> GetWords(string path, Func<string,Boolean> s)
{
string Text = "";
try
{
Text = File.ReadAllText(path).ToLower();
}
catch
{
Console.Write("File not found");
}
Char[] characters = { ' ',',','.','_', '\n' };
string[] array = Text.Split(characters);
Array.Sort(array, (first, last) => first.Length.CompareTo(last.Length));
foreach (string word in array)
{
if (s(word) == true)
yield return word;
}
}
I want to get the Array.Sort outside the GetWords method. Is there a simple way to do this?
>Solution :
I assume you mean, "I want to sort the result of GetWords, but thats not an Array, its an IEnumerable so I cant use Array.Sort – how do I do that?"
var list = GetWords(....);
var sorted = list.OrderBy(s=>s);
see How to sort an IEnumerable<string>