如何将2个对象与Icomparable进行比较

时间:2014-05-20 09:28:57

标签: c#

我有这个课程

{

public  class Conexiune:ICloneable,IComparable<Conexiune>
{
    [XmlElement("Name")]
    private string Nume;


   private string Server;

    private string User;

    private string Parola;

    private string NumeBD;

    public string NUME
    {
        get { return this.Nume; }
        set { this.Nume = value; }
    }

    public string SERVER
    {
        get { return this.Server; }
        set { this.Server = value; }
    }

    public string USER
    {
        get { return this.User; }
        set { this.User = value; }
    }

    public string PAROLA
    {
        get { return this.Parola; }
        set { this.Parola = value; }
    }

    public string NUMEBD
    {
        get { return this.NumeBD; }
        set { this.NumeBD = value; }
    }

    public override string ToString()
    {
        return Nume;
    }
    public Conexiune()
    {

    }
    public Conexiune(string numec, string serverc, string userc, string parolac, string numebdc)
    {
        this.Nume = numec;
        this.Server = serverc;
        this.User = userc;
        this.Parola = parolac;
        this.NumeBD = numebdc;
    }

    public object CloneP()
    {
        Conexiune copieP = new Conexiune(this.NUME, this.SERVER, this.USER, this.PAROLA, this.NumeBD);
        return copieP;
    }
    public object Clone()
    {
        Conexiune copie = new Conexiune();
        return copie;
    }

    //Here i want to compare
    public int CompareTo(Conexiune other)
    {
        int result = 0;
        return result;
    }
}
}

我想比较2个Conexiune(连接),但我不知道要比较什么。比较必须返回一个int

2 个答案:

答案 0 :(得分:1)

在这种情况下,只需使用NumeBD字段的字符串比较器:

public int CompareTo(Conexiune other)
{
    return String.Compare(NUMEBD, other.NUMEBD, StringComparison.Ordinal);
}

如果NumeBD可以null来正确订购,则可能需要对其进行修改

答案 1 :(得分:0)

如果希望在排序后将对象列出更高,则返回负数,如果希望它更低,则返回正数;如果对象相等,则返回0

我们不知道您的订单是什么