当我将WebGrid上的列从A排序到Z时,空字符串总是先出现,所以我有:
""
""
"A"
"B"
(当然没有引号)。
我希望他们最后出现,有一种简单的方法吗?
答案 0 :(得分:2)
你应该使用这样的通用比较委托进行排序:
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);