n..." />

Compare two lists or arrays of arbitrary length in C#; order is important?

String list1 = {"a", "c", "b", "d", "f", "e"}; string list2 = {"a", "d", "e", "f", "h"}; int I = 0; var list1a = list1. Intersect(list2). Select(l=> new { item = l, order= i++}); int j = 0; var list2a = list2.

Intersect(list1). Select(l=> new { item = l, order= j++}); var r = from l1 in list1a join l2 in list2a on l1. Order equals l2.

Order where l1. Item! = l2.

Item select new {result=string. Format("position {1} is item {0} on list1 but its {2} in list2", l1. Item, l1.

Order, l2. Item )}; r.Dump() result position 2 is item f on list1 but its e in list2 position 3 is item e on list1 but its f in list2.

String list1 = {"a", "c", "b", "d", "f", "e"}; string list2 = {"a", "d", "e", "f", "h"}; int I = 0; var list1a = list1. Intersect(list2). Select(l=> new { item = l, order= i++}); int j = 0; var list2a = list2.

Intersect(list1). Select(l=> new { item = l, order= j++}); var r = from l1 in list1a join l2 in list2a on l1. Order equals l2.

Order where l1. Item! = l2.

Item select new {result=string. Format("position {1} is item {0} on list1 but its {2} in list2", l1. Item, l1.

Order, l2. Item )}; r.Dump(); result position 2 is item f on list1 but its e in list2 position 3 is item e on list1 but its f in list2.

The way I understand this, is it only compares source sequences of equal length and corresponding elements (at equal indexes). In my case, my two sequences have different lengths and may be considered a match (or OK at any rate) even if elements with different indexes are different, providing the sequences are the same. – Notre Jun 15 '09 at 21:45 I noticed you weren't interested in comparing the sequences... the query above will produce the desired results .. try running in in LinqPad... – ehosca Jun 15 '09 at 22:39 Very nice!

I have a lot to learn about Linq. – Notre Jun 15 '09 at 22:54.

I don't have code, but this should be the two main steps: Remove every item that is in one list only (either shorten one of the lists or create a third "clean" list) Some kind of diff - search for the first mismatching item and then report every following item until both lists are "synchronized" again. Perhaps it will even help to clean up both of the lists. Then you can use a pointer for each list, set it to the first item and increase them until there's a mismatch.

I'm not sure I follow the first suggestion (with the two bullet points) but I guess my original problem description wasn't very clear. Sorry! What I'm trying right now is to iterate through both lists in a nested manner, to eliminate items not common to either list so that I end up with two new lists (or the original lists both modified) that contain just the content that exists in both lists, in the list original sequence.

I then take these two modified lists & use the pointer to each list idea, increasing the reference location until I find a mismatch (& then I record the mismatch). – Notre Jun 15 '09 at 22:04 I'd like to give both you & Vasu credit, but it looks like I can only mark one answer as correct. Although both very equally helpful for me :( – Notre Jun 15 '09 at 22:24.

I see my problem description wasn't very clear at all. LBushkin is right in that describing my expected results would've been helpful. (And now I understand why both you and ehosca suggested SequenceEquals -- the assumption being I just want a true/false answer).

But the idea of using Intersect will certainly cut down on some of the code I've written so thank you very much! – Notre Jun 15 '09 at 22:19.

I think the first solution that you have already accepted has a flaw. It will tell you everything is out of order even if one little thing is inserted: string list1 = { "a", "c", "b", "d", "j", "e", "f" }; string list2 = { "a", "d", "e", "f", "h", "j" }; that says that j, e, f are out of order, because the j was inserted. This points out the problem you face.

There are multiple solutions, even more than one optimal solution, to the problem of what is out of order. Is the J in order or the e and the f? Are they all out of order?

There is something called the Levenshtein distance algorithm that finds a minimal number of inserts and delete operations necessary to start with set A and end up with set B. There are multiple best solutions, this just finds one of them. This following algorithm correctly outputs that in list1, j was inserted and e, f are shifted but still in the correct order.

Using System; using System.Collections. Generic; using System. Text; using System.

Collections; using Math = System. Math; namespace LevCompareLists { class Program { static void Main(string args) { string list1 = { "a", "c", "b", "d", "j", "e", "f" }; string list2 = { "a", "d", "e", "f", "h", "j" }; int? AMap21 = Levenshtein(list2, list1); int?

AMap12 = Levenshtein(list1, list2); } public static int? Levenshtein(string Src, String Dst) { // this finds a minimum difference solution of inserts and deletes that maps Src to Dst // it returns the map from the perspective of Dst, i.e. : // each element of the return array contains the Src index of the corresponging element in B // a null value means the element in B was inserted, and never existed in A // // Given A = {"a", "c", "b", "d", "j", "e", "f"} // B = {"a", "d", "e", "f", "h", "j"}; // // Levenshtein(B, A): // a c be d j e f = 0) || (iDst >= 0)) { if ((iSrc > 0) && (iDst > 0)) { // enter here if iSrc and iDst are in the lev matrix and not on its edge int nCur = aLeviSrc, iDst; int nIns = nCur - aLeviSrc, iDst - 1; // if move along B to find match, it was an insert int nDel = nCur - aLeviSrc - 1, iDst; // if move along A to find match, it was a deletion if (nIns == 1) // this char was NOT in A, but was inserted into B iDst--; // Leave map of Bj to nowher, scan to previous B (--j) else if (nDel == 1) // this char was in A, but is missing in B iSrc--; // Don't map any B, scan to previous A (--i) else // Match aMapiDst-- - 1 = iSrc-- - 1; // After map Bj to Ai, scan to prev A,B (--i, --j) } else { if (iDst > 0) // remaining chars are inserts, Leave map of Bj to nowher, scan to previous B (--j) iDst--; else if (iSrc > 0) // Delete to the end, deletes do nothing iSrc--; else break; } } DumpMap(aMap, Dst); // Debug return aMap; } // just for debugging static void DumpLevMatrix(int, aLev, string Src, string Dst) { StringBuilder sb = new StringBuilder(); int cSrc = Src.

Length; int cDst = Dst. Length; int iSrc, iDst; sb. Length = 6; for (iDst = 0; iDst Length = 3; else { sb.

Length = 0; sb. AppendFormat("{0,-3}", SrciSrc - 1); } for (iDst = 0; iDst AppendFormat("{0:00}", aLeviSrc, iDst). Append(" "); Console.

WriteLine(sb.ToString()); } } // just for debugging static void DumpMap(int? AMap, string Dst) { StringBuilder sb = new StringBuilder(); for (int iMap = 0; iMap WriteLine(sb.ToString()); sb. Length = 0; for (int iMap = 0; iMap Append(" "); else sb.

AppendFormat("{0:00}", aMapiMap). Append(" "); Console. WriteLine(sb.ToString()); } } }.

How about this: string list1 = { "a", "c", "b", "d", "f", "e" }; string list2 = { "a", "d", "e", "f", "h" }; var indexedList1 = list1. Select((x, i) => new { Index = i, Item = x }); var indexedList2 = list2. Select((x, i) => new { Index = i, Item = x }); var intersectedWithIndexes = indexedList2 .

Join(indexedList1, x => x. Item, y => y. Item, (x, y) => new { ExpectedIndex = x.

Index, ActualIndex = y. Index, x. Item }) .

Where(x => x. ActualIndex! = x.

ExpectedIndex) .ToArray(); var outOfOrder = intersectedWithIndexes . Select((x, i) => new { Item = x, index = I }) . Skip(1) .

Where(x => x.Item. ActualIndex new { ExpectedBefore = x. Item, ExpectedAfter = intersectedWithIndexesx.

Index - 1 }); foreach (var item in outOfOrder) { Console. WriteLine("'{0}' and '{1}' are out of order at index {2}", item.ExpectedBefore. Item, item.ExpectedAfter.

Item, item.ExpectedBefore. ActualIndex); } output: 'f' and 'e' are out of order at index 4.

Yes, that works too. Thank you as well! – Notre Jun 16 '09 at 0:04.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions