Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / July 2007

Tip: Looking for answers? Try searching our database.

inconsistent ArrayList sort results

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
ChrisB - 26 Jul 2007 17:33 GMT
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
Jon Skeet [C# MVP] - 26 Jul 2007 17:50 GMT
<snip>

> Any idea why the order of the items in the InvoiceLineItemCollection is not
> consistent when sorting the same group of items?

What sort of inconsistency are you encountering? If two items have the
same plan ID and plan beneficiary assignment ID, then the order of them
isn't defined by your comparison - is that what you're seeing?

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

ChrisB - 26 Jul 2007 22:51 GMT
Jon,

While preparing a response to your question, I realized that the Guids on
which the sort is based vary each time the related unit test runs.  When
this is taken into account, the sort code appears to be working properly.

Thanks for your help.
Chris

> <snip>
>
[quoted text clipped - 11 lines]
> See http://www.pobox.com/~skeet/csharp/complete.html for details of
> what I mean by that.
Jon Skeet [C# MVP] - 26 Jul 2007 23:06 GMT
> While preparing a response to your question, I realized that the Guids on
> which the sort is based vary each time the related unit test runs.  When
> this is taken into account, the sort code appears to be working properly.
>
> Thanks for your help.

My pleasure. It's always nice when a solution can be found and my sole
contribution is hitting Ctrl-F1 which inserts the "Could you post
a..." text for me :)

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.