If you mean you want to sort by length, then the key itself as a string, try:
class LengthComparer : IComparer<String>
{
public int Compare(string x,string y)
{
int lengthComparison=x.Length.CompareTo(y.Length)
if(lengthComparison==0)
{
return x.CompareTo(y);
}
else
{
return lengthComparison;
}
}
}
What this code does is this: It makes a comparison based on length. If the two strings are tied for length, then it resolves the tie by comparing the strings themselves, not their length. You need to resolve this tie because of the reason @Adriano gave in his comment.