Compares two sequences. : IEnumerable « Collections Data Structure « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » Collections Data Structure » IEnumerableScreenshots 
Compares two sequences.
 

//GNU General Public License version 2 (GPLv2)
//http://dotwayutilities.codeplex.com/license
using System.Collections.Generic;

namespace Dotway.BuildTasks.Utils
{
    /// <summary>
    /// Compares two sequences.
    /// </summary>
    /// <typeparam name="T">Type of item in the sequences.</typeparam>
    /// <remarks>
    /// Compares elements from the two input sequences in turn. If we
    /// run out of list before finding unequal elements, then the shorter
    /// list is deemed to be the lesser list.
    /// </remarks>
    public class EnumerableComparer<T> : IComparer<IEnumerable<T>>
    {
        /// <summary>
        /// Object used for comparing each element.
        /// </summary>
        private readonly IComparer<T> comp;

        /// <summary>
        /// Create a sequence comparer using the default comparer for T.
        /// </summary>
        public EnumerableComparer()
        {
            comp = Comparer<T>.Default;
        }

        /// <summary>
        /// Create a sequence comparer, using the specified item comparer
        /// for T.
        /// </summary>
        /// <param name="comparer">Comparer for comparing each pair of
        /// items from the sequences.</param>
        public EnumerableComparer(IComparer<T> comparer)
        {
            comp = comparer;
        }

        #region IComparer<IEnumerable<T>> Members

        /// <summary>
        /// Compare two sequences of T.
        /// </summary>
        /// <param name="x">First sequence.</param>
        /// <param name="y">Second sequence.</param>
        public int Compare(IEnumerable<T> x, IEnumerable<T> y)
        {
            using (IEnumerator<T> leftIt = x.GetEnumerator())
            using (IEnumerator<T> rightIt = y.GetEnumerator())
            {
                while (true)
                {
                    bool left = leftIt.MoveNext();
                    bool right = rightIt.MoveNext();

                    if (!(left || right)) return 0;

                    if (!leftreturn -1;
                    if (!rightreturn 1;

                    int itemResult = comp.Compare(leftIt.Current, rightIt.Current);
                    if (itemResult != 0return itemResult;
                }
            }
        }

        #endregion
    }
}

   
  
Related examples in the same category
1.Determines whether the specified collection is null or empty.
2.Determines whether the collection contains the specified element
3.Determines whether the collection contains all the elements in the specified collection.
4.Aggregate IEnumerable
5.Convert IEnumerable To DataTable
6.Convert IEnumerable by Func
7.Split IEnumerable
8.Shuffle IEnumerable
9.For each IEnumerable
10.Sleep before yield each element in IEnumerable
11.Add WhereIf to IEnumerable
12.Add OrderBy to IEnumerable
13.Add Repeat extension to IEnumerable
14.Yield element in IEnumerable until Predicate
15.IEnumerable extension for get second and third element
16.Simple function to determine if an item is an IEnumerable
17.Object class extension for IEnumerable, IComparable and where
18.Output DataTable and IEnumerable to console
19.Convert IEnumerable to String
20.Convert XmlNodeList to IEnumerable
21.Returns the first element contained in both, source and candidates.
22.Given a range (start,end) and a number of steps, will yield that a number for each step
23.Empty Enumerable/Enumerator
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.