We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Sorting an Arraylist of Objects?

kylepike
kylepike asked
on
Medium Priority
222 Views
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)
            {
                  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()
                  {                        
                  }
            }


Comment
Watch Question

Commented:
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);
}
}

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Commented:
//  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

Commented:
thanks, I knew there had to be an easy solution!
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.