c# How to compare strings in class using CompareTo?

2950 views c#
1

sorry for previous post, so i will put my class code.

using System;

namespace U3_13
{
abstract class Narys
{
    public string Vardas { get; set; }
    public string Pavarde { get; set; }
    public DateTime GimimoData { get; set; }
    public Narys()
    {
    }
    public Narys(string vardas, string pavarde, DateTime gimimodata)
    {
        Vardas = vardas;
        Pavarde = pavarde;
        GimimoData = gimimodata;
    }
    public override bool Equals(object obj)
    {
        return this.Equals(obj as Narys);
    }
    public bool Equals(Narys narys)
    {
        if (Object.ReferenceEquals(narys, null))
        {
            return false;
        }
        if (this.GetType() != narys.GetType())
        {
            return false;
        }
        return (Vardas == narys.Vardas) && (Pavarde == narys.Pavarde);
    }
    public override int GetHashCode()
    {
        return Vardas.GetHashCode() ^ Pavarde.GetHashCode();
    }
    public static bool operator ==(Narys lhs, Narys rhs)
    {
        if (Object.ReferenceEquals(lhs, null))
        {
            if (Object.ReferenceEquals(rhs, null))
            {
                return true;
            }
            return false;
        }
        return lhs.Equals(rhs);
    }
    public static bool operator !=(Narys lhs, Narys rhs)
    {
        return !(lhs == rhs);
    }
    public static bool operator <=(Narys lhs, Narys rhs)
    {
        return lhs.Pavarde.CompareTo(rhs.Pavarde);
    }
    public static bool operator >=(Narys lhs, Narys rhs)
    {
        return (lhs.Pavarde.CompareTo() >= rhs.Pavarde.CompareTo());
    }
}
}

So as you see, at the end of the class, those two operators <= and >= are written wrong. I need to use especially CompareTo, because my task requires it, but i don't understand how it works. Some folk said to write:

return lhs.Pavarde.CompareTo(rhs.Pavarde);

But it gives me an error, so i need help. By the way, names are called in my Lithuanian language, i will give you better understanding.

Narys - Member;

Vardas - FirstName;

Pavarde - LastName;

GimimoData - birthdate;

So i need to sort firstly by the lastname and secondly by the firstname(if 2 has same lastnames).

answered question

It goes like this: lhs.CompareTo(rhs);. Returns negative if lhs is less, positive if greater, zero if they are equal.

Why would you overload >= and <= ? You have already overloaded ==, so I assume overloading > and < would give you these automatically.

1 Answer

12

have a look at the documentation which says about the result of CompareTo()

Less than zero This instance precedes value.

Zero This instance has the same position in the sort order as value.

Greater than zero This instance follows value. -or- value is null.

so you have to replace

public static bool operator <=(Narys lhs, Narys rhs)
{
    return lhs.Pavarde.CompareTo(rhs.Pavarde);
}
public static bool operator >=(Narys lhs, Narys rhs)
{
    return (lhs.Pavarde.CompareTo() >= rhs.Pavarde.CompareTo()); //syntax error here!
}

with

public static bool operator <=(Narys lhs, Narys rhs)
{
    return lhs.Pavarde.CompareTo(rhs.Pavarde) <= 0;
}
public static bool operator >=(Narys lhs, Narys rhs)
{
    return (lhs.Pavarde.CompareTo(rhs.Pavarde)) >= 0;
}

posted this

Have an answer?

JD

Please login first before posting an answer.