When sorting a column on my WebGrid from A to Z, the empty strings always come up first, so I have :
""
""
"A"
"B"
(Without the quotes of course).
I want them to come up last, is there a simple way to do this ?
When sorting a column on my WebGrid from A to Z, the empty strings always come up first, so I have :
""
""
"A"
"B"
(Without the quotes of course).
I want them to come up last, is there a simple way to do this ?
you should sort with generic Comparison delegate like this one:
private int MyComparison(string x, string y)
{
if (string.IsNullOrEmpty(x))
{
if (string.IsNullOrEmpty(y))
{
// If x is null and y is null, they're
// equal.
return 0;
}
else
{
// If x is null and y is not null, y
// is greater.
return 1;
}
}
else
{
// If x is not null...
//
if (string.IsNullOrEmpty(y))
// ...and y is null, x is greater.
{
return -1;
}
//sort them with ordinary string comparison
else
return x.CompareTo(y);
}
}
List<string> liststring = new List<string>() {"C", "","A","","B"};
liststring.Sort(MyComparison);