C# List Point 集合排序

时间:2022-09-03 18:54:13

 

要把 List<Point> pointsList{get;set;}

 先按X坐标,然后按Y坐标排序
有两个方法:
1.使用Icomparer:
class
PointComparer : IComparer < Point >
{
 
public int Compare ( Point first , Point second )
 
{
   
if ( first . X == second . X )
   
{
       
return first . Y - second . Y ;
   
}
   
else
   
{
       
return first . X - second . X ;
   
}

 
}
}
pointsList
.
Sort
(
new
 
PointComparer
());


2.使用LinQ:
pointsList . OrderBy ( p => p . X ). ThenBy ( p => p .Y ) ;