What is the best way to sort controls inside of a flowlayout panel?

I am adding custom controls to a FlowLayoutPanel. Each control has a date property. I would like to sort the controls in the flowlayoutpanel based on the date property. I can't presort the controls before I add them because it is possible for the user to add more.

My current thought is when the ControlAdded event for the FlowLayoutPanel is triggered I loop through the controls and use the BringToFront function to order the controls based on the date.

What is the best way to do this?

Answers


I doubt this is the best but is what I have so far:

        SortedList<DateTime,Control> sl = new SortedList<DateTime,Control>();
        foreach (Control i in mainContent.Controls)
        {
            if (i.GetType().BaseType == typeof(MyBaseType))
            {
                MyBaseType iTyped = (MyBaseType)i;
                sl.Add(iTyped.Date, iTyped);
            }
        }


        foreach (MyBaseType j in sl.Values)
        {
            j.SendToBack();
        }

Need Your Help

How can I increase the performance in a map lookup with key type std::string?

c++ dictionary optimization visual-c++ stdmap

I'm using a std::map (VC++ implementation) and it's a little slow for lookups via the map's find method.