Link to home
Start Free TrialLog in
Avatar of IzzyTwinkly
IzzyTwinklyFlag 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

ASKER CERTIFIED SOLUTION
Avatar of kaufmed
kaufmed
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
There are given valid solutions and one correction.

Sara