如何按面积对矩形列表进行排序?我已经在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);}}它是有效的,但它相当丑陋,而且我知道有一种更简单的方法...
发布于 2012-09-11 05:40:09
假设你可以使用LINQ,这样的东西应该可以工作:
var sortedList = Oblici.OrderBy(r => r.Width * r.Height).ToList();https://stackoverflow.com/questions/12359839
复制相似问题