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

C# How do I compare two sorted sets?

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

So in my program, I have a List with 1000 ticket objects. Each object has an int ID and an int SortedSet of 6 numbers. I want the user to be able to input six numbers, and for these 6 numbers to be compared to the 6 six numbers in the sorted set in each of the 1000 objects in the list. If the numbers match, I want the ID of the object to be output. What would be the best way of achieving this? Should I put the 6 numbers entered by the user into a SortedSet as well? That is what I was thinking of doing. If so, how would I compare the SortedSet to each of the 1000 SortedSets in my List? I've been working on this for two days and my head is fried lol!

Hope that made sense!

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

Yeah go ahead and put the users numbers into a SortedSet as well and then you can use the following method to see if a set in your ticket list is equal to the set with the user entry.

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

To get a list of IDs you could do something like this.

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