我有一个存储整数丢失的列表。 我不喜欢默认的List.Sort()工作,因为我希望列表按实际的int大小排序。 到目前为止,我有这个:优化列表<T>。排序(比较器)
哦,并且整数存储在字符串中,例如“1234”。这是我无法改变的。
public class IntComparer : IComparer<string>
{
public int Compare(string x, string y)
{
if (x == null)
{
if (y == null)
{
// 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 (y == null)
// ...and y is null, x is greater.
{
return 1;
}
else
{
// ...and y is not null, compare the
// lengths of the two strings.
//
int xInt = Convert.ToInt32(x);
int yInt = Convert.ToInt32(y);
if (x > y)
{
// If the strings are not of equal length,
// the longer string is greater.
//
return 1;
}
else if (xInt == yInt)
{
return 0;
}
else
{
// If the strings are of equal length,
// sort them with ordinary string comparison.
//
return -1;
}
}
}
}
但据我所知,这是泡沫排序,正确吗? 我应该实施什么呢?快速排序?另外,我可能需要编写它的帮助。
哦,我的列表中包含短存储在字符串
而且号码2000元,我打电话给我的IComparer是这样的:
IntComparer intSort = New IntComparer();
List<T>.Sort(intSort);
D'oh - 几乎相同,但你打我3分钟。 +1 ;-p – 2009-04-21 09:39:26