0

I have an arraylist with objects and their properties. Is there a way to compare the properties of the objects?

Update Below is the list example

listTA = {(ID, MonAry[], RequestDate), (ID, MonAry[], RequestDate)};
8
  • Yes, but please provide some example data (input and desirable results of comparation) Commented Apr 19, 2011 at 18:20
  • Yes. (Your question is so vague I don't even have a guess about what you're trying to ask) Commented Apr 19, 2011 at 18:20
  • A code sample would really help here. Commented Apr 19, 2011 at 18:21
  • 1
    The answer is yes. But you should define your preferred output so that the correct "yes" answer can be supplied. Commented Apr 19, 2011 at 18:21
  • I've updated the question. Hope its clear now. Commented Apr 19, 2011 at 18:24

2 Answers 2

2

Create a new Class Implementing IComparer Interface. then you can sort your list by calling myList.Sort(new MyComparer()); and you can compare each one with other one using new MyComparer().Compare(firstOne, secondOne);

sample :

using System;
using System.Collections;

public class SamplesArrayList  {

   public class myReverserClass : IComparer  {

      // Calls CaseInsensitiveComparer.Compare with the parameters reversed.
      int IComparer.Compare( Object x, Object y )  {
          // you can implement this method as you wish! cast your x and y objects and access to their properties.
          return( (new CaseInsensitiveComparer()).Compare( y, x ) );
      }

   }

   public static void Main()  {

      // Creates and initializes a new ArrayList.
      ArrayList myAL = new ArrayList();
      myAL.Add( "The" );
      myAL.Add( "quick" );
      myAL.Add( "brown" );
      myAL.Add( "fox" );
      myAL.Add( "jumps" );
      myAL.Add( "over" );
      myAL.Add( "the" );
      myAL.Add( "lazy" );
      myAL.Add( "dog" );

      // Displays the values of the ArrayList.
      Console.WriteLine( "The ArrayList initially contains the following values:" );
      PrintIndexAndValues( myAL );

      // Sorts the values of the ArrayList using the default comparer.
      myAL.Sort();
      Console.WriteLine( "After sorting with the default comparer:" );
      PrintIndexAndValues( myAL );

      // Sorts the values of the ArrayList using the reverse case-insensitive comparer.
      IComparer myComparer = new myReverserClass();
      myAL.Sort( myComparer );
      Console.WriteLine( "After sorting with the reverse case-insensitive comparer:" );
      PrintIndexAndValues( myAL );

   }

   public static void PrintIndexAndValues( IEnumerable myList )  {
      int i = 0;
      foreach ( Object obj in myList )
         Console.WriteLine( "\t[{0}]:\t{1}", i++, obj );
      Console.WriteLine();
   }

}

another IComparer sample :

private class sortYearAscendingHelper : IComparer
{
   int IComparer.Compare(object a, object b)
   {
      car c1=(car)a;
      car c2=(car)b;
      if (c1.year > c2.year)
         return 1;
      if (c1.year < c2.year)
         return -1;
      else
         return 0;
   }
}
Sign up to request clarification or add additional context in comments.

2 Comments

+1 If the OP is needing the comparison to sort, this is a great answer. :)
how should I do if I want to compare values inside the array(MonAry[]) with another object's MonAry[] ?
0

Check the SO post,

Arraylist can't compare objects after they are loaded from disk

Check the tutorial blog for either using IComparable OR IComparer2

http://www.codeproject.com/KB/recipes/Beginners_Sort.aspx#IComparablevsIComparer2

Thanks

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.