如何按面积对矩形列表进行排序?一直在 msdn 库中查看 IComparable,但无法弄清楚......我写了这个:
SortedL= new List<Rectangle>();
int count1 = 0;
int count3 = redovi;
while (count1 < count3)
{
int count2 = 0;
while (count2 < count3)
{
int x = Oblici[count1].Width;
int y = Oblici[count1].Height;
int z = Oblici[count2].Width;
int w = Oblici[count2].Height;
int area1 = x * y;
int area2 = z * w;
int a = area1.CompareTo(area2);
if (a < 0)
{
count1 = count2;
if (count2 < (count3 - 1))
{
count2++;
}
else break;
}
else if (a == 0)
{
if (count2 < (count3 - 1))
{
count2++;
}
else break;
}
else if (a > 0)
{
if (count2 < count3 - 1)
{
count2++;
}
else break;
}
}
SortedL.Add(Oblici[count1]);
Oblici.RemoveAt(count1);
count3 = (count3 - 1);}}
它有效,但它很丑陋,我知道有一种更简单的方法......