IComparer问题

目的:有多个选项可以使用我的类的属性进行排序。 我设法使用Comparable使用samAccountName排序,但未能正确实现IComparer。 但是知道我收到了下面指定的错误。

错误:未实现接口成员System.Collections.Icomparer

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections; namespace ActiveDirectory { public class sortLastName : IComparer { int IComparer.CompareTo(Employee oEmployee, Employee oEmployee2) { return String.Compare(oEmployee.lastName, oEmployee2.lastName); } public static IComparer sortYearAscending() { return (IComparer)new sortLastName(); } } //The code works great when I make a call like // List x = new List(); // x.sort();// sorts by samAccountName // Now I would like to figure out how to sort by lastName and still be able to sort by //samAccountName if necceaary. public class Employee :IComparable { //default sort order public int CompareTo(object oEmployee) { Employee emp1 = (Employee)oEmployee; return String.Compare(this.samAccountName, emp1.samAccountName); } public string lastName { get; set; } public string samAccountName { get; set; } } } 

你需要为你的sortLastName -class实现一个名为“ Compare ”而不是“CompareTo”的方法来实现IComparer

然后您可以使用此重载按x.Sort排序:

 x.Sort(new sortLastName()); 

这是您应该使用的实现:

 public class sortLastName : IComparer { public int Compare(Employee oEmployee, Employee oEmployee2) { return String.Compare(oEmployee.lastName, oEmployee2.lastName); } } 

(注意我没有更改名称,因为你不会再看到上下文了,但sortLastName不是比较器的好名字)

我完全不知道您的IComparer实现是什么。

您可能应该实现IComparable而不是非genericsIComparable ,但无论如何。

无论哪种方式,将您想要的排序顺序合并到CompareTo函数中。 例如:

 public int CompareTo(object oEmployee) { Employee e = (Employee)oEmployee; int cmp = 0; if ((cmp = string.Compare(this.samAccountName, e.samAccountName) != 0) return cmp; if ((cmp = string.Compare(this.lastName, e.lastName) != 0) return cmp; // ...any other properties you care to compare by // else it's a tie: return cmp; } 

然后这应该与List.Sort一起正常工作,因为默认比较器将在您的类型上查找IComparable的实现。

编辑:重新阅读时,我不确定您是要按一个标准,另一个标准或两者标准排序。 但是我会留下这个答案给后人,以防你想要两者排序。

IComparer的。 Compare将两个对象作为参数。 您不在class sortLastName中使用该签名。 还有一个通用版本,如果那是你想要的界面……

IComparer: http : //msdn.microsoft.com/en-us/library/system.collections.icomparer.compare( v=vs.71) .aspx

您根本不需要IComparer或IComparable来实现这一目标。

 List sortedByLastname = lstEmployee.OrderBy(x => x.LastName); 

实施://假设这与员工填写列表。 //在你的建议之后,这就是我最终做的事情。

 公共部分类Form1:表格
     {
        公共Form1()
         {
            的InitializeComponent();
             populatecboAdUsers();
         }
 public void populatecboAdUsers()
         {
            列表oEmployee = manager.getAllEmployees();
             //oEmployee.Sort();//使用samAccountName的默认比较

             IComparer myComparer = new Employee.compareEmployeeByLastName();
             oEmployee.Sort(myComparer.Compare);
             foreach(oEmployee中的var x)
             {

                 cboAdUsers.Items.Add(x.lastname);
                 Console.WriteLine(x.lastname);

        }
 } // Form1的结尾:表单类

 //下面的新课
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Text;
使用System.Collections;


命名空间ActiveDirectory
 {


    公共类员工:IComparable
     {
         public class compareEmployeeByLastName:IComparer
         {
             int IComparer.Compare(对象x,对象y)
             {
                员工emp1 =(员工)x;
                员工emp2 =(员工)y;
                 return String.Compare(emp1.lastname,emp2.​​lastname);
             }
         }
         //默认排序顺序
         public int CompareTo(object oEmployee)
         {
            员工emp1 =(员工)oEmployee;
             return String.Compare(this.samAccountName,emp1.samAccountName);
         }


         public string firstName 
         { 
            得到; 组; 
         }

         public string lastName
         {
            得到;
            组;

         }

         public string commonName
         {
            得到;
            组;
         }

        公共字符串部门
         {
            得到;
            组;
         }

         public string distinguishedName
         {
            得到;
            组;
         }

         public string employeeID
         {
            得到;
            组;
         }

         public string samAccountName
         {
            得到;
            组;
         }

        公共字串电子邮件
         {
            得到;
            组;
         }

        公共字符串标题
         {
            得到;
            组;
         }

         public UserPrincipalExtension oUserPrincipalExtension
         {
            得到;
            组;
         }
     }



 }