I have a BindingList with objects in which is saved some contacts.
internal static BindingList<object> contactList;
The contactList object is more types. SktContact, FBContact, GContact.
These contacts I add in contactList_Changed event to listBox. But I want sort them by status (first online) and then alphabetically.
I would like somethink like this:
List<object> contactSorted = new List<object>();
contactSorted = Global.contactList.ToList();
contactSorted.OrderBy((status, name) => Converter.getContactAvailabilityStatus(contactSorted[?]), Converter.getContactName(contactSorted[?]));
contactListBox.Items.AddRange(contactSorted.ToArray());
But I dont have any idea how shoud I do that. I cannot retype the list of objects and select the property to order by.
I have methods to get status and name from any object. But how can I use it to sort the contact array?
I'm using own drawing Owner draw fixed
private void contactListBox_DrawItem(object sender, DrawItemEventArgs e)
What you can do is to implement an interface for your contact classes.
And change your class declarations as
After that you can declare your list with the interface name
Then order the list as
Then while using the list items you can use GetType() method to determine their types and use the appropriate code to handle them.
In case if you don't have access to the implementations of the contact classes you can use Adapter Design Pattern to make them compatible with your code.