Sort List <Rectangle>

Posted by user1649498 on Stack Overflow See other posts from Stack Overflow or by user1649498
Published on 2012-09-10T21:36:21Z Indexed on 2012/09/10 21:37 UTC
Read the original article Hit count: 135

Filed under:

How do I sort a rectangle list by their area? Been looking into IComparable at msdn library, but can't figure it out... I wrote this:

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);}}

And it works, but it's pretty ugly, and I know there is an easier way...

© Stack Overflow or respective owner

Related posts about c#