IzzyTwinkly
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?
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));
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Sara