Want more features on Pastebin? Sign Up, it's FREE!
Guest

Fastest way to compare two lists

By: a guest on Feb 27th, 2012  |  syntax: None  |  size: 0.93 KB  |  views: 16  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. List<string> l1 = new List<string> {"Hello", "World","How","Are","You"};
  2. List<string> l2 = new List<string> {"Hello","World","How","Are","You"};
  3. Console.WriteLine(l1.SequenceEqual(l2));
  4.        
  5. public static bool CompareLists(List<int> l1, List<int> l2)
  6. {
  7.         if (l1 == l2) return true;
  8.         if (l1.Count != l2.Count) return false;
  9.         for (int i=0; i<l1.Count; i++)
  10.                 if (l1[i] != l2[i]) return false;
  11.         return true;
  12. }
  13.        
  14. public static CompareList<T>(IList<T> obj1, IList<T> obj2, Action<T,T> match)
  15. {
  16.    if (obj1.Count != obj2.Count) return false;
  17.    for (int i = 0; i < obj1.Count; i++)
  18.    {
  19.      if (obj2[i] != null && !match(obj1[i], obj2[i]))
  20.        return false;
  21.    }
  22. }
  23.        
  24. var testList = userInput.match(/[-|w]+/g)  
  25.         /*the above catches common errors:
  26.          using dash or starting with a numeric*/
  27.     listToUse = userInput.match(/[a-zA-Z]w*/g)
  28.  
  29.     if (listToUse.join(" ") != testList.join(" ")) {
  30.                 return "the lists don't match"
clone this paste RAW Paste Data