本文介绍了排序时如何在末尾放置空字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在WebGrid上从A到Z排序一列时,空字符串总是最先出现,所以我有:
When sorting a column on my WebGrid from A to Z, the empty strings always come up first, so I have :
""
""
"A"
"B"
(当然没有引号).
我希望他们最后出现,有一种简单的方法吗?
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);
这篇关于排序时如何在末尾放置空字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!