Avatar of IzzyTwinkly
IzzyTwinkly
Flag for United States of America

asked on 

How do I compare IOrderedEnumerable<char> in C#?

Hi,

I have the following code to check if two string are the same after they are re-ordered.
I want to use LINQ and this is what I did.
when I print s1 and s2 using foreach...they are both "eilnst". But it returns "false".

How can I correct this?

static void Main(string[] args)
        {
            string str1 = "Silent";
            string str2 = "Listen";
            Console.WriteLine(areEqual(str1, str2));
            Console.Read();
        }
        
        public static bool areEqual(string str1, string str2)
        {
            var s1 = str1.ToLower().OrderBy(x => x);
            var s2 = str1.ToLower().OrderBy(x => x);
            foreach (var item in s1)
            {
                Console.Write(item);

            }
            Console.WriteLine();
            foreach (var item in s2)
            {
                Console.Write(item);
            }
            Console.WriteLine();

            return  str1.ToLower().OrderBy(x => x)==(str1.ToLower().ToList().OrderBy(x => x));          
        }

Open in new window

.NET ProgrammingC#

Avatar of undefined
Last Comment
sarabande

8/22/2022 - Mon