Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Sort array outside method

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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>

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading