Warm tip: This article is reproduced from serverfault.com, please click

list-C#如何比较两个排序集?

(list - C# How do I compare two sorted sets?)

发布于 2016-02-19 22:28:52

所以在我的程序中,我有一个包含 1000 个票证对象的列表。每个对象都有一个 int ID 和一个由 6 个数字组成的 int SortedSet。我希望用户能够输入六个数字,并将这 6 个数字与列表中 1000 个对象中每个对象的排序集中的 6 个六个数字进行比较。如果数字匹配,我希望输出对象的 ID。实现这一目标的最佳方法是什么?我是否也应该将用户输入的 6 个数字也放入 SortedSet 中?这就是我想做的事情。如果是这样,我将如何将 SortedSet 与列表中的 1000 个 SortedSet 中的每一个进行比较?我已经为此工作了两天,我的头都炸了哈哈!

希望这是有道理的!

Questioner
badgerbadger
Viewed
0
Bilal Bashir 2016-02-20 07:22:36

是的,继续将用户编号也放入 SortedSet 中,然后你可以使用以下方法查看你的票证列表中的集合是否等于包含用户条目的集合。

SortedSet<int>.CreateSetComparer().Equals(userSet, objectSet);

要获取 ID 列表,你可以执行以下操作。

IEnumerable<int> GetMatchingSetIDs(SortedSet<int> userSet)
    {
        IEqualityComparer<SortedSet<int>> setComparer = SortedSet<int>.CreateSetComparer();
        foreach (Ticket ticket in tickets) //Where ticket is your ticket class with the sortedsets and tickets is a List of tickets.
        {
            if (setComparer.Equals(ticket.Set, userSet))
            {
                yield return ticket.ID;
            }
        }
    }