Compares two lists. Considers list equal if two lists contains the same elements, element order is ignored. - CSharp System.Collections.Generic

CSharp examples for System.Collections.Generic:IList

Description

Compares two lists. Considers list equal if two lists contains the same elements, element order is ignored.

Demo Code

/********************************************************************
 *  FulcrumWeb RAD Framework - Fulcrum of your business             *
 *  Copyright (c) 2002-2010 FulcrumWeb, ALL RIGHTS RESERVED         *
 *                                                                  *
 *  THE SOURCE CODE CONTAINED WITHIN THIS FILE AND ALL RELATED      *
 *  FILES OR ANY PORTION OF ITS CONTENTS SHALL AT NO TIME BE        *
 *  COPIED, TRANSFERRED, SOLD, DISTRIBUTED, OR OTHERWISE MADE       *
 *  AVAILABLE TO OTHER INDIVIDUALS WITHOUT EXPRESS WRITTEN CONSENT  *
 *  AND PERMISSION FROM FULCRUMWEB. CONSULT THE END USER LICENSE    *
 *  AGREEMENT FOR INFORMATION ON ADDITIONAL RESTRICTIONS.           *
 ********************************************************************/
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Collections;
using System;/*from   w  ww . j  a  v a2 s.com*/

public class Main{
        //-------------------------------------------------------------------------
    /// <summary>
    /// Compares two lists. 
    /// Considers list equal if two lists contains the same elements,
    /// element order is ignored.
    /// </summary>
    /// <typeparam name="T">type of list elements</typeparam>
    /// <param name="l1">first list</param>
    /// <param name="l2">second list</param>
    /// <returns>true if lists are equal, false otherwise</returns>
    static public bool CompareUnOrdered<T>(IList<T> l1, IList<T> l2)
    {
      if (l1 == null && l2 == null)
      {
        return true;
      }
      if (l1 == null || l2 == null)
      {
        return false;
      }
      if (l1.Count != l2.Count)
      {
        return false;
      }
      foreach (T item in l1)
      {
        if (!l2.Contains(item))
        {
          return false;
        }
      }
      foreach (T item in l2)
      {
        if (!l1.Contains(item))
        {
          return false;
        }
      }
      return true;
    }
}

Related Tutorials