寻求允许自定义排序方法的类

发布于 2024-07-17 04:40:10 字数 544 浏览 7 评论 0原文

我编写了一个小应用程序,它允许我比较两个数据库模式以检查结构差异。 它一直深入到列详细信息,这很好。 当我比较非常小的数据库时,我没有遇到任何问题,但是当我开始比较具有数百个表的数据库时,上下滚动以查找错误所在是很烦人的。 最初,我使用 KeyedCollection 来保存表的列表,但由于它不允许任何类型的排序,因此我已更改为 SortedList。 SortedList 确实对索引进行排序,这给了我按字母顺序排列的表,但这比我需要的要少。 我需要的是一个允许我根据对象属性(而不仅仅是索引值)进行排序的类。 为了便于使用,我想将有错误的表推到列表的开头。 任何人都知道我可以使用哪个课程来完成此任务?

EDIT1:List 类没有索引,这对我的类来说至关重要。 我尝试使用字典,它有索引,但没有排序方法。 实现 IComparer 不起作用,因为构造函数只接受索引类型的 IComparer。 由于 IComparer 是另一个无法访问列表类的内部值列表的类,因此我无法根据对象属性进行比较。

EDIT2:我需要一个按对象属性而不是索引值对索引进行排序的类,但我开始相信这样的类不存在。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

撞了怀 2024-07-24 04:40:10

然后你需要创建一个类来实现System.Collections.Generic.IComparer,然后你可以将所有对象放入一个支持基于IComparer排序的集合中,例如:

System.Collections.Generic.List将允许您根据自己的实现进行排序。

这里有一个链接,它是一个很好的示例,可以帮助您理解这一点。

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.

述情 2024-07-24 04:40:10

使用重载构造函数和自定义IComparer 类,您可以根据需要实现排序。

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

命比纸薄 2024-07-24 04:40:10

如果您想要做的只是创建自己的排序,那么这可能是最简单的:

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

如果您不想使用匿名方法并希望在多个位置进行相同类型的排序,您也可以这样做:

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

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);
热血少△年 2024-07-24 04:40:10

没有简单的方法可以做到这一点。 也许甚至不是一条艰难的路。 由于它的目的是用于生成输出,因此我决定对部分输出进行多个循环以达到我想要的结果。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文