Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.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# - Fatal编程技术网

C# 比较问题

C# 比较问题,c#,C#,用途:使用my类的属性具有多个排序选项。 我使用Comparable成功地使用samAccountName进行排序,但未能正确实现IComparer。但我知道我得到了下面指定的错误 错误:未实现接口成员System.Collections.Icomparer 您需要为sortLastName类实现一个名为not CompareTo的方法 然后,您可以按x排序。按以下方式排序: 这是您应该使用的实现: public class sortLastName : IComparer<Employe

用途:使用my类的属性具有多个排序选项。 我使用Comparable成功地使用samAccountName进行排序,但未能正确实现IComparer。但我知道我得到了下面指定的错误

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


您需要为sortLastName类实现一个名为not CompareTo的方法

然后,您可以按x排序。按以下方式排序:

这是您应该使用的实现:

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

注意:我没有更改名称,因为您将无法再看到上下文,但是sortLastName对于比较器来说不是一个好名称

我完全不知道IComparer实现的用途

您可能应该实现IComparable而不是非泛型IComparable,但不管怎样

无论哪种方式,都可以将所需的排序顺序合并到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的实现


编辑:在重读时,我不确定是否要按一个标准、另一个标准或两者进行排序。但我会把这个答案留给后人,以防你们想同时按这两个方面进行排序。

I比较者。比较以两个对象作为参数。在类sortLastName中不使用该签名。还有一个通用版本,如果这是你想要的接口

I比较者:
你根本不需要IComparer或IComparable来实现这一点

List<Employee> sortedByLastname = lstEmployee.OrderBy(x => x.LastName);
实施: //假设这将用员工填写列表。 //在你的建议之后,这就是我最后要做的

public partial class Form1 : Form { public Form1() { InitializeComponent(); populatecboAdUsers(); } public void populatecboAdUsers() { List oEmployee = manager.getAllEmployees(); //oEmployee.Sort();//uses the default compare by samAccountName IComparer myComparer = new Employee.compareEmployeeByLastName(); oEmployee.Sort(myComparer.Compare); foreach (var x in oEmployee) { cboAdUsers.Items.Add(x.lastname); Console.WriteLine(x.lastname); } }//end of Form1:form class //new class below using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections; namespace ActiveDirectory { public class Employee :IComparable { public class compareEmployeeByLastName : IComparer { int IComparer.Compare(object x, object y) { Employee emp1 = (Employee)x; Employee emp2 = (Employee)y; return String.Compare(emp1.lastname, emp2.lastname); } } //default sort order public int CompareTo(object oEmployee) { Employee emp1 = (Employee)oEmployee; return String.Compare(this.samAccountName, emp1.samAccountName); } public string firstName { get; set; } public string lastName { get; set; } public string commonName { get; set; } public string department { get; set; } public string distinguishedName { get; set; } public string employeeID { get; set; } public string samAccountName { get; set; } public string email { get; set; } public string title { get; set; } public UserPrincipalExtension oUserPrincipalExtension { get; set; } } }
他给出的一个用例是将列表排序到位,这是一件合理的事情,你是对的。但这不是我的问题,我知道我可以让List newList=oEmployee.OrderByx=>x.samAccountName.ToList;你确定你明白这个问题吗?当我读到这篇文章时,他想通过SamaAccountName进行比较,但也可以选择按姓氏排序-您的解决方案将按AccountName排序,然后再按姓氏排序-但正如我猜测的那样:AccountName将是唯一的,因此这没有多大意义不,我可能误解了它。我在答案的底部添加了一个注释。需要通用版本作为列表的输入。排序。。。请参见EmployeeLastNameComparer或CompareeEmployeeByLastName
List<Employee> sortedByLastname = lstEmployee.OrderBy(x => x.LastName);
public partial class Form1 : Form { public Form1() { InitializeComponent(); populatecboAdUsers(); } public void populatecboAdUsers() { List oEmployee = manager.getAllEmployees(); //oEmployee.Sort();//uses the default compare by samAccountName IComparer myComparer = new Employee.compareEmployeeByLastName(); oEmployee.Sort(myComparer.Compare); foreach (var x in oEmployee) { cboAdUsers.Items.Add(x.lastname); Console.WriteLine(x.lastname); } }//end of Form1:form class //new class below using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections; namespace ActiveDirectory { public class Employee :IComparable { public class compareEmployeeByLastName : IComparer { int IComparer.Compare(object x, object y) { Employee emp1 = (Employee)x; Employee emp2 = (Employee)y; return String.Compare(emp1.lastname, emp2.lastname); } } //default sort order public int CompareTo(object oEmployee) { Employee emp1 = (Employee)oEmployee; return String.Compare(this.samAccountName, emp1.samAccountName); } public string firstName { get; set; } public string lastName { get; set; } public string commonName { get; set; } public string department { get; set; } public string distinguishedName { get; set; } public string employeeID { get; set; } public string samAccountName { get; set; } public string email { get; set; } public string title { get; set; } public UserPrincipalExtension oUserPrincipalExtension { get; set; } } }