sorting comma delimited strings
Ok
If I have 2 strings :
strA = "1,2,3" and strB = "3,2,1"
and I compare them they obviously wont equal eachother.
However in my logig because they both contain the same 3 numbers I want to be able to say well yes man they do equal eachother.
This would require that I sort both strings.
I have seen a number of approaches but none seem too concise.
Any Ideas ?
Thanks in Advance
Re: sorting comma delimited strings
I just came across this which seems pretty tidy :
Code:
string input = "1,9,3,4,3,9,1,5,6,78,";
string[] stringArray = input.Split( new char[] { ',' },
StringSplitOptions.RemoveEmptyEntries );
int length = stringArray.Length;
int[] intArray = new int[length];
for (int i = 0; i < length; i++)
{
try
{
intArray[i] = Convert.ToInt32( stringArray[i] );
}
catch (Exception)
{
// ignore
}
}
Array.Sort( intArray );
Re: sorting comma delimited strings
Tidy, but does it do what you expect? I'd be tempted to take both arrays, Split(","), then iterate through one in a fashion like this:
vb Code:
A = String1.Split(",") 'Array 1
B = String2.Split(",") 'Array 2
'A quick check.
if A.Length<>B.Length then
'They aren't the same.
End if
'A slower check
'Since they are both string arrays, this should work:
Array.Sort(A)
Array.Sort(B)
For x=0 to A.Length-1
If A(x) <> B(x) Then
'THey aren't the same
Exit For
End If
Next