C
ChrisB
Hello:
I am trying to sort a collection using the sorting functionality offered by
the ArrayList but seem to be getting inconsistent results.
--------------------------------------------------------------------------------
the InvoiceLineItemCollection sort code looks like this -
internal void Sort()
{
// Sort collection items using ArrayList.
// Collection items must implement IComparable.
ArrayList arrayList = new ArrayList();
foreach(InvoiceLineItem invoiceLineItem in this)
{
arrayList.Add(invoiceLineItem.Clone());
}
arrayList.Sort();
// Repopulate collection with sorted items.
this.Clear();
foreach(Object obj in arrayList)
{
this.Add((InvoiceLineItem)obj);
}
}
----------------------------------------------------------------------------------------
The InvoiceLineItem implementation of CompareTo looks like this -
(Note: both PlanID and PlanBeneficairyID are of type GUID).
public int CompareTo(Object obj)
{
if(!(obj is InvoiceLineItem)) throw new InvalidCastException("Not an
InvoiceLineItem object.");
Int32 sortResult;
InvoiceLineItem invoiceLineItem = (InvoiceLineItem)obj;
// * Perform line Item sort. *
// Notes:
// 1. If items are equal (result = 0), comparison is based upon
// next search criteria property.
// 2. Sort criteria for line Items are:
// a. PlanID
// b. PlanBeneficiaryAssignmentID
// Compare plans(sort criteria 1).
sortResult = this.PlanID.CompareTo(invoiceLineItem.PlanID);
if(sortResult == 0)
{
// Compare plan beneficiary assignments (sort criteria 2).
sortResult =
this.PlanBeneficiaryAssignmentID.CompareTo(invoiceLineItem.PlanBeneficiaryAssignmentID);
}
return sortResult;
}
----------------------------------------------------
Any idea why the order of the items in the InvoiceLineItemCollection is not
consistent when sorting the same group of items?
Thanks!
Chris
I am trying to sort a collection using the sorting functionality offered by
the ArrayList but seem to be getting inconsistent results.
--------------------------------------------------------------------------------
the InvoiceLineItemCollection sort code looks like this -
internal void Sort()
{
// Sort collection items using ArrayList.
// Collection items must implement IComparable.
ArrayList arrayList = new ArrayList();
foreach(InvoiceLineItem invoiceLineItem in this)
{
arrayList.Add(invoiceLineItem.Clone());
}
arrayList.Sort();
// Repopulate collection with sorted items.
this.Clear();
foreach(Object obj in arrayList)
{
this.Add((InvoiceLineItem)obj);
}
}
----------------------------------------------------------------------------------------
The InvoiceLineItem implementation of CompareTo looks like this -
(Note: both PlanID and PlanBeneficairyID are of type GUID).
public int CompareTo(Object obj)
{
if(!(obj is InvoiceLineItem)) throw new InvalidCastException("Not an
InvoiceLineItem object.");
Int32 sortResult;
InvoiceLineItem invoiceLineItem = (InvoiceLineItem)obj;
// * Perform line Item sort. *
// Notes:
// 1. If items are equal (result = 0), comparison is based upon
// next search criteria property.
// 2. Sort criteria for line Items are:
// a. PlanID
// b. PlanBeneficiaryAssignmentID
// Compare plans(sort criteria 1).
sortResult = this.PlanID.CompareTo(invoiceLineItem.PlanID);
if(sortResult == 0)
{
// Compare plan beneficiary assignments (sort criteria 2).
sortResult =
this.PlanBeneficiaryAssignmentID.CompareTo(invoiceLineItem.PlanBeneficiaryAssignmentID);
}
return sortResult;
}
----------------------------------------------------
Any idea why the order of the items in the InvoiceLineItemCollection is not
consistent when sorting the same group of items?
Thanks!
Chris