Finding a List of Anagrams in C#

There are no words for the horror.
Oct 29 2009 by James Craig

This is a very simple algorithm that I created for a game that I'm working on. The game is a word game for smaller children and one of the things that it needs to do is anagrams. So I had to come up with a function to help me find them:

 public static Dictionary<string, System.Collections.Generic.List<string>> GetAnagramEquivalents(System.Collections.Generic.List<string> InputArray)
{
Dictionary<string, System.Collections.Generic.List<string>> ReturnList = new Dictionary<string, System.Collections.Generic.List<string>>();
for (int x = 0; x < InputArray.Count; ++x)
{
char[] InputCharArray=InputArray[x].ToCharArray();
Array.Sort(InputCharArray);
string InputString = new string(InputCharArray);
if (ReturnList.ContainsKey(InputString))
{
ReturnList[InputString].Add(InputArray[x]);
}
else
{
ReturnList.Add(InputString, new System.Collections.Generic.List<string>());
ReturnList[InputString].Add(InputArray[x]);
}
}
return ReturnList;
}

The code above is very basic.  But the general idea is that with a set of words, if you sort the letters in alphabetical order you end up with an equivalent string. For instance both car and arc would be acr when sorted. Thus they are equivalent and anagrams of one another. So all you need to do is pass in a list of words and it passes back a dictionary of anagrams. That's all there is to it. So try it out, leave feedback, and happy coding.