1

我需要制作升序的通用列表。这是我的通用类型:

public class ContainerData
{
   private Point pnt;
   private Size size;
   private Double Area;
   private Contour<Point> contour;

   public ContainerData()
   { }

   public ContainerData(ContainerData containerData)
   { 
     this.pnt=containerData.pnt;
     this.size=containerData.size;
     this.Area=containerData.Area;
     this.contour = containerData.contour;
   }

   public Point pointProperty
   {
       get
       {return pnt;}
       set
       {pnt = value;}
   }

   public Size sizeProperty
   {
       get
       {return size;}
       set
       {size = value;}
   }

   public Double AreaProperty
   {
       get
       {return Area;}
       set
       {Area = value;}
   }
   public Contour<Point> ContourProperty
   {
       get
       {return contour;}
       set
       { contour = value; }
   }
}

升序排序必须在第 th Point 类型的 X 坐标值下进行。

这是应该排序的类成员:

 private List<ContainerData> dataOfPreviusImage;

知道如何实施吗?

先感谢您。

4

3 回答 3

9

使用Enumerable.OrderBy

List<ContainerData> dataOfPreviusImage = GetSomeData();
var sorted = dataOfPreviusImage.OrderBy(cd => cd.pointProperty.X);
于 2012-10-11T08:12:23.020 回答
2

做一个比较功能:

private int SortFunction(ContainerData obj1, ContainerData obj2)
{
    return obj1.pointProperty.X.CompareTo(obj2.pointProperty.X);
}

并这样称呼它:

dataOfPreviusImage.Sort(new Comparison<ContainerData>(SortFunction));
于 2012-10-11T08:13:23.873 回答
1

为了帮助您一路走好,您的 ContainerData 类也应该实现 IComparable 和理想情况下的 IEquateable。

于 2012-10-11T08:15:50.070 回答