读书人

关于Listlt;gt;Sort不能排序的有关问题

发布时间: 2011-12-29 22:09:38 作者: rapoo

关于List<>.Sort不能排序的问题
以下代码 不能正确 排序 为什么

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
List<point> ps = new List<point>();
ps.Add(new point(0.0012));
ps.Add(new point(0.000002));
ps.Add(new point(0.4));
ps.Add(new point(0.00002));
ps.Add(new point(0.098));
ps.Sort((point p1, point p2) =>
{
if (p1.x - p2.x < 1e-10)
return 0;
if (p1.x > p2.x)
return 1;
else
return -1;

});
foreach (point p in ps)
Console.WriteLine(p);
}


}

public class point
{
public double x;
public point(double d)
{
x = d;
}
public override string ToString()
{
return x.ToString();
}
}



}


[解决办法]
ps.Sort((point p1, point p2) =>
{
if (System.Math.Abs(p1.x - p2.x) < 1e-10) return 0;
if (p1.x > p2.x)
return 1;
else
return -1;

});

读书人网 >.NET

热点推荐