Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 比较数组列表中的对象_C#_Sorting_Collections_Compare - Fatal编程技术网

C# 比较数组列表中的对象

C# 比较数组列表中的对象,c#,sorting,collections,compare,C#,Sorting,Collections,Compare,我有一个包含对象及其属性的arraylist。有没有办法比较对象的属性 更新下面是列表示例 listTA = {(ID, MonAry[], RequestDate), (ID, MonAry[], RequestDate)}; 创建一个实现IComparer接口的新类。然后,您可以通过调用myList.sort(new MyComparer())对列表进行排序并且您可以使用newMyComparer()将每一个与另一个进行比较 样本: using System; using System.C

我有一个包含对象及其属性的arraylist。有没有办法比较对象的属性

更新下面是列表示例

listTA = {(ID, MonAry[], RequestDate), (ID, MonAry[], RequestDate)};

创建一个实现
IComparer
接口的新类。然后,您可以通过调用
myList.sort(new MyComparer())对列表进行排序
并且您可以使用new
MyComparer()将每一个与另一个进行比较

样本:

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

}
另一个IComparer示例:

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;
   }
}
private class sortYearAscendingHelper:IComparer
{
int IComparer.Compare(对象a、对象b)
{
汽车c1=(汽车)a;
汽车c2=(汽车)b;
如果(c1年>c2年)
返回1;
如果(c1年
检查SO帖子

查看教程博客,了解如何使用
IComparable
icomparare2


谢谢

是,但请提供一些示例数据(输入和理想的比较结果)是。(你的问题太模糊了,我甚至都猜不出你想问什么)代码示例在这里真的很有用。答案是肯定的。但是您应该定义您的首选输出,以便提供正确的“是”答案。我已经更新了问题。希望它现在清楚了。+1如果OP需要比较来排序,这是一个很好的答案。:)如果要将数组(MonAry[])内的值与另一个对象的MonAry[]进行比较,该如何做?