• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 176
  • Last Modified:

Generic lists - compare

I have 2 generic lists List<>. I would like to check if they are identical.
How do I do that?
0
johnkainn
Asked:
johnkainn
  • 2
3 Solutions
 
abelallenCommented:
Hi,

Please go through the following program...

using System;  
using System.Collections.Generic;
using System.Linq;
namespace ConsoleApplication1 {
     class ListCompareTest
     {
         static void Main()
         {
             List<String> First = new List<string>() { "apple", "orange", "grape" };
             List<String> Second = new List<string>() { "orange", "grape", "apple" };
             Second.Sort();
             First.Sort();
             bool trial = First.SequenceEqual<String>(Second);
             Console.WriteLine(trial);
             Console.Read();
         }
     }

 }


Hope this will help you to understand the concept.


Regards,
Shyju PG
0
 
lenordisteCommented:
Above method definitely works but if you use more complex types than strings then you will have to implement your own comparer for it to work. Here is the msdn doc on how to do that:
http://msdn.microsoft.com/en-us/library/bb342073.aspx
0
 
anarki_jimbelCommented:
It seems abelallen's code has some errors. And definitely does not compile in VS2005.

Try my sample code. It has some comments so I hope it'll be clear:
        private bool areEqual(List<String> first, List<String> second)
        {
            bool result = false;
            // simple check to avoid extra work
            if (first.Count == second.Count)
            {
                // create a dictionary and add first list to it as keys
                Dictionary<string, string> hash = new Dictionary<string, string>();
                foreach (string ele in first)
                {
                    hash.Add(ele, "");
                }
                // check each element from the second list is there
                result = true;
                foreach (string ele in second)
                {
                    if(!hash.ContainsKey(ele))
                    {
                        result = false;
                        break; 
                    }
                }
            }
            return result;
        }
        private void button1_Click(object sender, EventArgs e)
        {

             List<String> first = new List<string>(new string[] { "apple", "orange", "grapes", "banana" });
             List<String> second = new List<string> (new string[]{ "orange", "banana", "grape", "apple" });
            bool areListsEqual = areEqual(first, second);
            MessageBox.Show(areListsEqual.ToString());

         }

Open in new window

0
 
abelallenCommented:
Hi anarki,

Thanks for your comment. I knew it was a trivial solution. I thought of giving a small tablet for the light fever... :)



Regards,
Abel Allen
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now