Sorting an Arraylist of Objects?

Posted on 2006-04-04
Last Modified: 2010-04-16
I have an Array of objects that i am copying to an arraylist.

the object contains a public property called m_CurrentRaceMTP. it's an int.

i need to sort it ascending. i cannot change the object model to implement icomparable because there are other ways one would want to sort this object.

any thoughts? i have found some bubblesort and quicksort algorhythms but i need a bit of hand-holding to get to actually using something like NSort.

here's the code.  ideally, i'd like to sort m_Tracks next...

private static      ArrayList            m_Tracks            = new ArrayList();

public void InitForTracks(Program[] programs)
                        for(int h=0; h<programs.Length; h++)
                              if(programs[h].ProgramStatus == ProgramStatus.Unknown || programs[h].ProgramStatus == ProgramStatus.Available || programs[h].ProgramStatus == ProgramStatus.Open)


Question by:kylepike
    LVL 7

    Accepted Solution

    You can define your own System.Collections.IComparer and pass it in to the ArrayList.Sort() method.

    Here is an article with source code:

    Your IComparer would look something like
    public class CurrentRaceComparer : IComparer

    public int Compare(object x, object y)
        TypeStoredInm_Tracks p1, p2;

         p1 = x as TypeStoredInm_Tracks;
         p2 = y as TypeStoredInm_Tracks;

       return int p1.m_CurrentRaceMTP.CompareTo(p2.m_CurrentRaceMTP);
    LVL 7

    Expert Comment

    //  use this
    return  p1.m_CurrentRaceMTP.CompareTo(p2.m_CurrentRaceMTP);

    // or sort in other direction
    return p2.m_CurrentRaceMTP.CompareTo(p1.m_CurrentRaceMTP);

    Your client code would look something like
    m_Tracks.Sort(new CurrentRaceComparer());

    Author Comment

    thanks, I knew there had to be an easy solution!

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now