开发者

IComparer problem

开发者 https://www.devze.com 2023-04-01 02:03 出处:网络
Purpose: To have multiple options to sort using properties of my class . I managed to sort using samAccountName using Comparable but failed to correctly implement IComparer. But know I am getting an

Purpose: To have multiple options to sort using properties of my class . I managed to sort using samAccountName using Comparable but failed to correctly implement IComparer. But know I am getting an error specified below.

Error:Does not implement interface member 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, oEmploye开发者_StackOverflow社区e2.lastName);
        }
        public static IComparer sortYearAscending()
        {
            return (IComparer)new sortLastName();
        }

    }
//The code works great when I make a call like 
// List<Employee> x = new List<Employee>();
// 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;
        }
    }
}


you need to implement a method named "Compare" not "CompareTo" for your sortLastName-class implementing IComparer

then you can sort by x.Sort with this overload:

x.Sort(new sortLastName());

this is the implementation you should use:

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

(Note I didn't change the name because you wouldn't see the context any more but sortLastName is no good name for a comparer)


I have absolutely no idea what your IComparer implementation is for.

You should probably implement IComparable<T> instead of non-generic IComparable, but whatever.

Either way, incorporate the sort order you want into your CompareTo function. For example:

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

Then this should work correctly with List<T>.Sort, since the default comparer will look for an implementation of IComparable on your type.

EDIT: On a re-read, I'm not sure whether you want to sort by one criteria, the other, or both. But I'll leave this answer in for posterity in case you want to sort by both.


IComparer. Compare takes two object as argument. You don't use that signature in class sortLastName. There also is a generic version, if that's the interface You want...

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


You dont need IComparer or IComparable to achieve this at all.

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


Implementation: //suppose that this fill out the list with employees. //After your suggestions this is what I ended up doing.

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



}
0

精彩评论

暂无评论...
验证码 换一张
取 消