I have an Observable Collection of about 5,000 objects returning from a SQL query to my WCF service. I have been asked to sort the collection by 3 fields in the object. For brevity, lets say this is my c# model:
public class TestObject{
public string type1{get; set;}
public string type2{get; set;}
public string type3(get; set;}
}
And here is some test data that I have to try to sort:
public ObservableCollection<TestObject> TestObjects = new ObservableCollection<TestObject>();
TestObjects.Add(new TestObject('708','4','(A)'));
TestObjects.Add(new TestObject('7','41B','1(A)'));
TestObjects.Add(new TestObject('69','2','45'));
TestObjects.Add(new TestObject('708','4','(B)'));
TestObjects.Add(new TestObject('69','2','5'));
TestObjects.Add(new TestObject('7','41','1(B)'));
TestObjects.Add(new TestObject('7','41',''));
Is there a way to dynamically sort these so the Observable collection comes back sorted like this?
{(7,41,''),(7,41,1(B)),(7,41B,1(A)),(69,2,5),(69,2,45),(708,4,(A)),(708,4,(B))}
So far I have been able to sort them by the type1, but then trying to sort by the type2 screws the order up from the type1 sort. Here is the method I used to try to sort, followed by how I call it:
protected virtual ObservableCollection<TestObject> SortTestObjects(ObservableCollection<TestObjects> unsortedObjects, string level){
var SortedObjects = new ObservableCollection<TestObject>();
Comparison<TestObject> numericComp;
Comparison<TestObject> comparison;
Comparison<TestObject> AlphaNumericComp;
bool sortNumeric = false;
switch (level)
{
case "type1":
numericComp = (a, b) =>
{
var aKey = Convert.ToDouble(a.type1);
var bKey = Convert.ToDouble(b.type1);
return aKey.CompareTo(bKey);
};
AlphaNumericComp = (a, b) =>
{
return string.CompareOrdinal(a.type1, b.type1);
};
sortNumeric = unsortedObjects.ToList().TrueForAll(i => i.type1.IsNumeric());
break;
case "type2":
numericComp = (a, b) =>
{
var aKey = Convert.ToDouble(a.type2);
var bKey = Convert.ToDouble(b.type2);
return aKey.CompareTo(bKey);
};
AlphaNumericComp = (a, b) =>
{
return string.CompareOrdinal(a.type2, b.type2);
};
sortNumeric = unsortedObjects.ToList().TrueForAll(i => i.type2.IsNumeric());
break;
case "type3":
numericComp = (a, b) =>
{
var aKey = Convert.ToDouble(a.type3);
var bKey = Convert.ToDouble(b.type3);
return aKey.CompareTo(bKey);
};
AlphaNumericComp = (a, b) =>
{
return string.CompareOrdinal(a.type3, b.type3);
};
sortNumeric = unsortedObjects.ToList().TrueForAll(i => i.type3.IsNumeric());
break;
default:
numericComp = (a, b) =>
{
var aKey = Convert.ToDouble(a.type1);
var bKey = Convert.ToDouble(b.type1);
return aKey.CompareTo(bKey);
};
AlphaNumericComp = (a, b) =>
{
return string.CompareOrdinal(a.type1, b.type1);
};
sortNumeric = unsortedObjects.ToList().TrueForAll(i => i.type1.IsNumeric());
break;
}
comparison = sortNumeric ? numericComp : AlphaNumericComp;
unsortedObjects.ToList().Sort(comparison);
foreach(var obj in unsortedOjects){
SortedObjects.Add(obj)
}
return SortedObjects;
}
Public ObservableCollection<TestObject> SortAllTestObjects(){
var sort1 = SortTestObjects(TestObjects, "type1");
var sort2 = SortTestObjects(sort1, "type2");
var sort3 = SortTestObjects(sort2, "type3");
return sort3;
}
**Edit: Here is a SQL query that I would be using in this example. I am totally good with changing the query as long as I get the order correct in the client **
Select type1, type2, type3 from dbo.testObject tO where tO.del_date is null
Please let me know if you need any more info. Any help or constructive feedback would be awesome!