I have a case where I need to keep the order of one list the same as the order of another separate list. Both lists contain unique objects, but I have a field in each that I can use to match on.
Heres a simple example. The Name field in Class1 can be used as a key to match on CustName in Class2. Two lists are created and not in order, the iterative code removes a non matching item from Class2 and inserts it at the current index.
This works ok, but Im thinking that there are more elegant approaches, maybe using LINQ or some of the newer c# sharp features.
I welcome any suggestions or improvements.
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; using System;
<span style="color:Blue; using System.Collections.Generic;
<span style="color:Blue; using System.Linq;
<span style="color:Blue; using System.Text;
<span style="color:Blue; using System.Diagnostics;
<span style="color:Blue; namespace ObjectSynchronizer
{
<span style="color:Blue; class Class1
{
<span style="color:Blue; public <span style="color:Blue; string Name { <span style="color:Blue; get; <span style="color:Blue; set; }
<span style="color:Blue; public <span style="color:Blue; string Address { <span style="color:Blue; get; <span style="color:Blue; set; }
<span style="color:Blue; public <span style="color:Blue; string Phone { <span style="color:Blue; get; <span style="color:Blue; set; }
}
<span style="color:Blue; class Class2
{
<span style="color:Blue; public <span style="color:Blue; string CustName { <span style="color:Blue; get; <span style="color:Blue; set; }
<span style="color:Blue; public DateTime CustSince { <span style="color:Blue; get; <span style="color:Blue; set; }
<span style="color:Blue; public <span style="color:Blue; string Phone { <span style="color:Blue; get; <span style="color:Blue; set; }
}
<span style="color:Blue; class Program
{
<span style="color:Blue; static <span style="color:Blue; void Main(<span style="color:Blue; string[] args)
{
List<Class1> Customers = <span style="color:Blue; new List<Class1>
{
<span style="color:Blue; new Class1{ Name = <span style="color:#A31515; "Bob", Address = <span style="color:#A31515; "123 Main", Phone= <span style="color:#A31515; "902-333-5555"},
<span style="color:Blue; new Class1{ Name = <span style="color:#A31515; "Macy", Address = <span style="color:#A31515; "456 Main", Phone= <span style="color:#A31515; "902-333-6666"},
<span style="color:Blue; new Class1{ Name = <span style="color:#A31515; "Gertie", Address = <span style="color:#A31515; "111 Main", Phone= <span style="color:#A31515; "902-222-5555"},
<span style="color:Blue; new Class1{ Name = <span style="color:#A31515; "Floyd", Address = <span style="color:#A31515; "222 Main", Phone= <span style="color:#A31515; "902-444-5555"},
<span style="color:Blue; new Class1{ Name = <span style="color:#A31515; "Andre", Address = <span style="color:#A31515; "9 Main", Phone= <span style="color:#A31515; "902-123-5555"},
<span style="color:Blue; new Class1{ Name = <span style="color:#A31515; "Lloyd", Address = <span style="color:#A31515; "123 Perch", Phone= <span style="color:#A31515; "902-333-7654"},
<span style="color:Blue; new Class1{ Name = <span style="color:#A31515; "Beryl", Address = <span style="color:#A31515; "45 Sub", Phone= <span style="color:#A31515; "312-333-5555"},
<span style="color:Blue; new Class1{ Name = <span style="color:#A31515; "Marc", Address = <span style="color:#A31515; "33 Broad", Phone= <span style="color:#A31515; "202-333-5555"},
<span style="color:Blue; new Class1{ Name = <span style="color:#A31515; "Sandy", Address = <span style="color:#A31515; "3 Main", Phone= <span style="color:#A31515; "802-123-5555"},
<span style="color:Blue; new Class1{ Name = <span style="color:#A31515; "Coleman", Address = <span style="color:#A31515; "5 Trout", Phone= <span style="color:#A31515; "902-888-5555"},
};
List<Class2> LateCustomers = <span style="color:Blue; new List<Class2>
{
<span style="color:Blue; new Class2{ CustName = <span style="color:#A31515; "Gertie", CustSince=<span style="color:Blue; new DateTime(2001,10,01), Phone= <span style="color:#A31515; "902-222-5555"},
<span style="color:Blue; new Class2{ CustName = <span style="color:#A31515; "Bob", CustSince=<span style="color:Blue; new DateTime(2001,10,01), Phone= <span style="color:#A31515; "902-333-5555"},
<span style="color:Blue; new Class2{ CustName = <span style="color:#A31515; "Floyd", CustSince=<span style="color:Blue; new DateTime(2001,1,01), Phone= <span style="color:#A31515; "902-444-5555"},
<span style="color:Blue; new Class2{ CustName = <span style="color:#A31515; "Lloyd", CustSince=<span style="color:Blue; new DateTime(2001,3,01), Phone= <span style="color:#A31515; "902-333-7654"},
<span style="color:Blue; new Class2{ CustName = <span style="color:#A31515; "Beryl", CustSince=<span style="color:Blue; new DateTime(2001,4,01), Phone= <span style="color:#A31515; "312-333-5555"},
<span style="color:Blue; new Class2{ CustName = <span style="color:#A31515; "Andre", CustSince=<span style="color:Blue; new DateTime(2001,2,01), Phone= <span style="color:#A31515; "902-123-5555"},
<span style="color:Blue; new Class2{ CustName = <span style="color:#A31515; "Marc", CustSince=<span style="color:Blue; new DateTime(2001,5,01), Phone= <span style="color:#A31515; "202-333-5555"},
<span style="color:Blue; new Class2{ CustName = <span style="color:#A31515; "Macy", CustSince=<span style="color:Blue; new DateTime(2001,10,01), Phone= <span style="color:#A31515; "902-333-6666"},
<span style="color:Blue; new Class2{ CustName = <span style="color:#A31515; "Sandy", CustSince=<span style="color:Blue; new DateTime(2001,6,01), Phone= <span style="color:#A31515; "802-123-5555"},
<span style="color:Blue; new Class2{ CustName = <span style="color:#A31515; "Coleman", CustSince=<span style="color:Blue; new DateTime(2001,3,01), Phone= <span style="color:#A31515; "902-888-5555"}
};
<span style="color:Blue; for (<span style="color:Blue; int i = 0; i < Customers.Count(); i++)
{
<span style="color:Blue; if (LateCustomers.ElementAt(i).CustName != Customers.ElementAt(i).Name)
{
Class2 movedCustomer = LateCustomers.Where(c => c.CustName == Customers.ElementAt(i).Name).FirstOrDefault();
LateCustomers.Remove(movedCustomer);
LateCustomers.Insert(i, movedCustomer);
}
}
<span style="color:Blue; for (<span style="color:Blue; int i = 0; i < Customers.Count(); i++ )
{
Debug.Assert(Customers.ElementAt(i).Name == LateCustomers.ElementAt(i).CustName);
}
}
}
}
[/code] <hr class="sig Hedley
View the full article
Heres a simple example. The Name field in Class1 can be used as a key to match on CustName in Class2. Two lists are created and not in order, the iterative code removes a non matching item from Class2 and inserts it at the current index.
This works ok, but Im thinking that there are more elegant approaches, maybe using LINQ or some of the newer c# sharp features.
I welcome any suggestions or improvements.
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; using System;
<span style="color:Blue; using System.Collections.Generic;
<span style="color:Blue; using System.Linq;
<span style="color:Blue; using System.Text;
<span style="color:Blue; using System.Diagnostics;
<span style="color:Blue; namespace ObjectSynchronizer
{
<span style="color:Blue; class Class1
{
<span style="color:Blue; public <span style="color:Blue; string Name { <span style="color:Blue; get; <span style="color:Blue; set; }
<span style="color:Blue; public <span style="color:Blue; string Address { <span style="color:Blue; get; <span style="color:Blue; set; }
<span style="color:Blue; public <span style="color:Blue; string Phone { <span style="color:Blue; get; <span style="color:Blue; set; }
}
<span style="color:Blue; class Class2
{
<span style="color:Blue; public <span style="color:Blue; string CustName { <span style="color:Blue; get; <span style="color:Blue; set; }
<span style="color:Blue; public DateTime CustSince { <span style="color:Blue; get; <span style="color:Blue; set; }
<span style="color:Blue; public <span style="color:Blue; string Phone { <span style="color:Blue; get; <span style="color:Blue; set; }
}
<span style="color:Blue; class Program
{
<span style="color:Blue; static <span style="color:Blue; void Main(<span style="color:Blue; string[] args)
{
List<Class1> Customers = <span style="color:Blue; new List<Class1>
{
<span style="color:Blue; new Class1{ Name = <span style="color:#A31515; "Bob", Address = <span style="color:#A31515; "123 Main", Phone= <span style="color:#A31515; "902-333-5555"},
<span style="color:Blue; new Class1{ Name = <span style="color:#A31515; "Macy", Address = <span style="color:#A31515; "456 Main", Phone= <span style="color:#A31515; "902-333-6666"},
<span style="color:Blue; new Class1{ Name = <span style="color:#A31515; "Gertie", Address = <span style="color:#A31515; "111 Main", Phone= <span style="color:#A31515; "902-222-5555"},
<span style="color:Blue; new Class1{ Name = <span style="color:#A31515; "Floyd", Address = <span style="color:#A31515; "222 Main", Phone= <span style="color:#A31515; "902-444-5555"},
<span style="color:Blue; new Class1{ Name = <span style="color:#A31515; "Andre", Address = <span style="color:#A31515; "9 Main", Phone= <span style="color:#A31515; "902-123-5555"},
<span style="color:Blue; new Class1{ Name = <span style="color:#A31515; "Lloyd", Address = <span style="color:#A31515; "123 Perch", Phone= <span style="color:#A31515; "902-333-7654"},
<span style="color:Blue; new Class1{ Name = <span style="color:#A31515; "Beryl", Address = <span style="color:#A31515; "45 Sub", Phone= <span style="color:#A31515; "312-333-5555"},
<span style="color:Blue; new Class1{ Name = <span style="color:#A31515; "Marc", Address = <span style="color:#A31515; "33 Broad", Phone= <span style="color:#A31515; "202-333-5555"},
<span style="color:Blue; new Class1{ Name = <span style="color:#A31515; "Sandy", Address = <span style="color:#A31515; "3 Main", Phone= <span style="color:#A31515; "802-123-5555"},
<span style="color:Blue; new Class1{ Name = <span style="color:#A31515; "Coleman", Address = <span style="color:#A31515; "5 Trout", Phone= <span style="color:#A31515; "902-888-5555"},
};
List<Class2> LateCustomers = <span style="color:Blue; new List<Class2>
{
<span style="color:Blue; new Class2{ CustName = <span style="color:#A31515; "Gertie", CustSince=<span style="color:Blue; new DateTime(2001,10,01), Phone= <span style="color:#A31515; "902-222-5555"},
<span style="color:Blue; new Class2{ CustName = <span style="color:#A31515; "Bob", CustSince=<span style="color:Blue; new DateTime(2001,10,01), Phone= <span style="color:#A31515; "902-333-5555"},
<span style="color:Blue; new Class2{ CustName = <span style="color:#A31515; "Floyd", CustSince=<span style="color:Blue; new DateTime(2001,1,01), Phone= <span style="color:#A31515; "902-444-5555"},
<span style="color:Blue; new Class2{ CustName = <span style="color:#A31515; "Lloyd", CustSince=<span style="color:Blue; new DateTime(2001,3,01), Phone= <span style="color:#A31515; "902-333-7654"},
<span style="color:Blue; new Class2{ CustName = <span style="color:#A31515; "Beryl", CustSince=<span style="color:Blue; new DateTime(2001,4,01), Phone= <span style="color:#A31515; "312-333-5555"},
<span style="color:Blue; new Class2{ CustName = <span style="color:#A31515; "Andre", CustSince=<span style="color:Blue; new DateTime(2001,2,01), Phone= <span style="color:#A31515; "902-123-5555"},
<span style="color:Blue; new Class2{ CustName = <span style="color:#A31515; "Marc", CustSince=<span style="color:Blue; new DateTime(2001,5,01), Phone= <span style="color:#A31515; "202-333-5555"},
<span style="color:Blue; new Class2{ CustName = <span style="color:#A31515; "Macy", CustSince=<span style="color:Blue; new DateTime(2001,10,01), Phone= <span style="color:#A31515; "902-333-6666"},
<span style="color:Blue; new Class2{ CustName = <span style="color:#A31515; "Sandy", CustSince=<span style="color:Blue; new DateTime(2001,6,01), Phone= <span style="color:#A31515; "802-123-5555"},
<span style="color:Blue; new Class2{ CustName = <span style="color:#A31515; "Coleman", CustSince=<span style="color:Blue; new DateTime(2001,3,01), Phone= <span style="color:#A31515; "902-888-5555"}
};
<span style="color:Blue; for (<span style="color:Blue; int i = 0; i < Customers.Count(); i++)
{
<span style="color:Blue; if (LateCustomers.ElementAt(i).CustName != Customers.ElementAt(i).Name)
{
Class2 movedCustomer = LateCustomers.Where(c => c.CustName == Customers.ElementAt(i).Name).FirstOrDefault();
LateCustomers.Remove(movedCustomer);
LateCustomers.Insert(i, movedCustomer);
}
}
<span style="color:Blue; for (<span style="color:Blue; int i = 0; i < Customers.Count(); i++ )
{
Debug.Assert(Customers.ElementAt(i).Name == LateCustomers.ElementAt(i).CustName);
}
}
}
}
[/code] <hr class="sig Hedley
View the full article