Sorting an Arraylist of Objects?

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)
            {
                  try
                  {
                        m_Tracks.Clear();
                        for(int h=0; h<programs.Length; h++)
                        {
                              if(programs[h].ProgramStatus == ProgramStatus.Unknown || programs[h].ProgramStatus == ProgramStatus.Available || programs[h].ProgramStatus == ProgramStatus.Open)
                              {
                                    if(!m_Tracks.Contains(programs[h]))
                                          m_Tracks.Add(programs[h]);
                              }
                        }


                        }
                  catch()
                  {                        
                  }
            }


kylepikeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Here is an article with source code:
http://codebetter.com/blogs/david.hayden/archive/2005/03/06/56584.aspx

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);
}
}
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
mjmarlowCommented:
//  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());
0
kylepikeAuthor Commented:
thanks, I knew there had to be an easy solution!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.