.NET 3.5 and later provide the Enumeralbe.SequenceEqual method which can be handy in a case like this:
using System.Linq;
string svalue = "bbadc";
string[] arr = new string[]{"baddc", "abcdb", "aabbd", "caadb"};
private bool TryParseStringMatch(string sval, string[] sary, out string match, out int ndx)
{
sval = new string(sval.OrderBy(ch => ch).ToArray());
bool isMatch = false;
match = "";
ndx = -1;
for(int i = 0; i < sary.Length; i++)
{
string str = sary[i];
isMatch = sval.SequenceEqual(new string(str.OrderBy(ch => ch).ToArray()));
if (isMatch)
{
ndx = i;
match = str;
return true;
}
}
return isMatch;
}
string amatch;
int ndex;
if (TryParseStringMatch(svalue, arr, out amatch, out ndex))
{
}
If this method was something being called frequently, or was used to compare some string with a very large list of possible matches, I would order the strings by using the technique shown in Peter Vegter's answer here: converting the string to a char[], then sorting that array ... rather than, as shown here, using Linq to sort.
I've chosen to implement this method in a way that imitates the frequent .NET methods that begin with 'TryParse.