There are two ways of doing this - you either need to implement an
IComparer<string>
, or a Comparison delegate. The second method can easily be done as a lambda delegate in your call to the sort method.
listcode.Sort(delegate(string1, string2) {
int num1, num2;
try {
num1 = int.Parse(string1.SubString(0, string1.IndexOf('^')));
num2 = int.Parse(string2.SubString(0, string2.IndexOf('^')));
return num1.CompareTo(num2);
}
catch {
}
});
Otherwise, create a class that implements IComparer<string>. This class will have a
Compare(string1, string2)
method in it, which will pretty much be identical to the lambda delegate above. Then you call
listcode.Sort(new MyComparerClass());
. Look at
the MSDN documentation for IComparer[
^] for an example of how to implement.