-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCompareLists.cs
120 lines (102 loc) · 4.22 KB
/
CompareLists.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
using List_App.Resources.Models;
using System;
using System.Collections.Generic;
namespace List_App
{
public static class CompareLists
{
public static List<GroceryListClass> Compare(List<GroceryListClass> listA,
List<GroceryListClass> listB)
{
List<GroceryListClass> combinedListsLST = new List<GroceryListClass>();
// first, if there is a list in one that is not in other, just copy it
foreach (GroceryListClass a in listA)
{
foreach (GroceryListClass anyList in listB)
{
if (a.Name == anyList.Name)
goto ContinueLoop;
}
combinedListsLST.Add(a);
ContinueLoop:;
}
foreach (GroceryListClass b in listB)
{
foreach (GroceryListClass anyList in listA)
{
if (b.Name == anyList.Name)
goto ContinueLoop;
}
combinedListsLST.Add(b);
ContinueLoop:;
}
foreach (GroceryListClass any in combinedListsLST)
{
if (listA.Contains(any))
listA.Remove(any);
if (listB.Contains(any))
listB.Remove(any);
}
// now lt's compare the similar lists against each other
foreach (GroceryListClass anyListA in listA)
{
List<ItemClass> thisListResultItems = new List<ItemClass>();
GroceryListClass counterPartList = new GroceryListClass();
DateTime combinedTime = DateTime.UtcNow;
// first let's check the deleted status
foreach (GroceryListClass anyListB in listB)
{
if (anyListB.Name == anyListA.Name)
{
counterPartList = anyListB;
break;
}
}
// then let's check the items
foreach (ItemClass anyItem in anyListA.Items)
{
// let's compare items from one list to another
foreach (ItemClass counterItem in counterPartList.Items)
{
if (anyItem.Name == counterItem.Name)
{
// item exists both sides, let's decide which one to add
if (DateTime.Parse(anyItem.Time) > DateTime.Parse(counterItem.Time))
thisListResultItems.Add(anyItem);
else
thisListResultItems.Add(counterItem);
goto ContinueHere;
}
}
// if we reach here, non of the names have matchedd
thisListResultItems.Add(anyItem);
ContinueHere:;
}
// by now all items of this LIST is resolved, now, let's deal with counter part
foreach (ItemClass anyCounterItem in counterPartList.Items)
{
// let's compare items from counter to the main
foreach (ItemClass anyItem in anyListA.Items)
{
if (anyCounterItem.Name == anyItem.Name)
{
// item exists both sides, we have already added that, let's drop out
goto ContinueHere;
}
}
// if we reach here, non of the names have matched
thisListResultItems.Add(anyCounterItem);
ContinueHere:;
}
// this shopping class now contains all of the similar ones and uniques ones
combinedListsLST.Add(new GroceryListClass
{
Name = anyListA.Name,
Owner = anyListA.Owner,
Items = thisListResultItems
});
}
return combinedListsLST;
}
}
}