views:

171

answers:

4

I wrote a small app that allows me to compare two database schemas to check for differences in structure. It goes all the way down to column details, which is nice. When I was comparing very small databases, I had no trouble but when I start to compare databases with hundreds of tables, it is annoying to scroll up and down to find where the errors are. Originally, I was using KeyedCollection to hold the table's list, but since it doesn't allow any kind of sort, I have since changed to SortedList. SortedList does sort the indexes, which gives me the tables in alphabetical order, but this is less than what I need. What I need is a class that allows me to sort based on object properties, not only on the index values. I want to push the tables with errors to the beggining of the list for easy-of-use's sake. Anyone has an idea of which class I could use to accomplish this?

EDIT1: The List class doesn't have an index, which is kind of vital for my class. I tried using Dictionary, which does have an index, but doesn't have a sort method. Implementing IComparer doesn't work because the constructor only accepts IComparer of the index type. And since the IComparer is another class that doesn't have access to the inner value list of my list class, I can't compare based on the object properties.

EDIT2: I need a class that sorts the index by the object properties, not by the index values, but I am starting to believe such class does not exist.

A: 

Construct the SortedList using this overloaded constructor with a custom IComparer class that you implement to sort how you wish.

Ben S
+2  A: 

Then you need create an class to implement : System.Collections.Generic.IComparer, Then you can put all your object into a collection which support sorting based on IComparer, for example:

System.Collections.Generic.List will allow you to sort based on your own implementation.

Here is a link to a good example to help you understand this.

J.W.
A: 

If all you're trying to do is create your own sorting then this might be the easiest:

List<Foo> myFoos = new List<Foo>();
//--Write code to fill the list

//sort by ID
myFoos.Sort(delegate(Foo foo1, Foo foo2)
{
    return foo1.ID.CompareTo(foo2.ID);
});

//Sort by Last Name
myFoos.Sort(delegate(Foo foo1, Foo foo2)
{
    return foo1.LastName.CompareTo(foo2.LastName);
});

If you don't want to use Anonymous methods and want to have the same type of sorting in multiple places you can also do this:

public class FooIDCompare : IComparer<Foo>
    {
        #region IComparer<foo> Members

        public int Compare(Foo x, Foo y)
        {
            return x.ID.CompareTo(y.ID);
        }

        #endregion
    }

FooIDCompare idCompare = new FooIDCompare();
myFoos.Sort(idCompare);
Erick
A: 

There is no easy way to do that. Maybe not even a hard way. Since it was meant to be used to make output, I decided to instead make multiple loops of partial output to reach the results I wanted.

Leahn Novash