-1

I'm looking for a way to create a method which should compare for example 2 strings with 1 comparable string and return 1, 0 or -1 (if the value is greater than, equal to, or less than). The method should look like this:

int CompareEnumerables<T>(IEnumerable<T> xs, IEnumerable<T> ys, IComparer<T> comp)

This is the condition: write method (method of general comparison of 2 random sequences (the objects of which are of the same type allowing comparison))

int CompareEnumerables<T>(IEnumerable<T> xs, IEnumerable<T> ys, iComparer<T> comp) 

to return -1, 0 or 1 depending on which a series of lexicographical precedes the other.

Any idea how to do so? I tried a few times. If needed I will share code.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
RRuseva
  • 25
  • 5
  • I'm not clear on what you want here. What should the method return and why? – ProgrammingLlama Sep 16 '21 at 11:27
  • So you are looking for SequenceEqual? https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.sequenceequal?view=net-5.0 – Rand Random Sep 16 '21 at 11:27
  • The method should return 1, 0 or -1 (if the value is greater than, equal to, or less than) - by that I think its about the string length. And by looking at the documentation for IComparer it should use the method Compare. – RRuseva Sep 16 '21 at 11:32
  • Already tried with SequenceEqual - is not that – RRuseva Sep 16 '21 at 11:33
  • So what should "abc", "bwe" return? – Magnus Sep 16 '21 at 11:34
  • What did you try ? If you show us what you tried, it is easier to understand your requirements. And maybe we can show you where your attempt failed. – AndrewR Sep 16 '21 at 11:42
  • "abc","bwe" should return 0 (equal). From the documentation for IComparer.Compare those values can be seen. however my method should have for example: "abc", "bwe" as the two strings and "mnb" as a string to use to compare. In this case the result should be again 0 – RRuseva Sep 16 '21 at 11:42
  • https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.icomparer-1.compare?view=net-5.0 – RRuseva Sep 16 '21 at 11:43
  • So "abc","bwe" are equal? I dont understand that or how the IComparer plays into that. – Magnus Sep 16 '21 at 11:44
  • Yes, they are equal because there are 3 letters in each of them – RRuseva Sep 16 '21 at 11:45
  • public static int CompareEnumerables (IEnumerable xs, IEnumerable ys, IComparer comp) { List listToCompare1 = new List(xs); List listToCompare2 = new List(ys); return Comparer.DefaultInvariant.Compare(listToCompare1, listToCompare2); } - this is the first code that I tried – RRuseva Sep 16 '21 at 11:46
  • But what do you use the IComparer for if you are just comparing lengths? – Magnus Sep 16 '21 at 11:48
  • Its in the condition – RRuseva Sep 16 '21 at 11:50
  • 2
    Mabay you should just post the entire requirement as given to you in the question. – Magnus Sep 16 '21 at 11:54

1 Answers1

0

How about something like this:

public class SequenceComparer<T> : IComparer<IEnumerable<T>>
{
    public SequenceComparer(IComparer<T> inner = default)
    {
        Inner = inner ?? Comparer<T>.Default;
    }
    
    public IComparer<T> Inner { get; }
    
    public int Compare(IEnumerable<T> left, IEnumerable<T> right)
    {
        if (left is null) throw new ArgumentNullException(nameof(left));
        if (right is null) throw new ArgumentNullException(nameof(right));
        
        using (var leftEnum = left.GetEnumerator())
        using (var rightEnum = right.GetEnumerator())
        {
            bool leftRunning = leftEnum.MoveNext();
            bool rightRunning = rightEnum.MoveNext();
            while (leftRunning && rightRunning)
            {
                int result = Inner.Compare(leftEnum.Current, rightEnum.Current);
                if (result != 0) return result;
                
                leftRunning = leftEnum.MoveNext();
                rightRunning = rightEnum.MoveNext();
            }
            
            if (leftRunning) return 1;
            if (rightRunning) return -1;
            return 0;
        }
    }
}
static int CompareEnumerables<T>(IEnumerable<T> xs, IEnumerable<T> ys, IComparer<T> comp)
{
    var comparer = new SequenceComparer<T>(comp);
    return comparer.Compare(xs, vs);
}
Richard Deeming
  • 29,830
  • 10
  • 79
  • 151