[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


Sorting an Arraylist of Objects?

Posted on 2006-04-04
Medium Priority
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
  • 2

Accepted Solution

mjmarlow earned 1000 total points
ID: 16375877
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);

Expert Comment

ID: 16375952
//  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

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

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

872 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