Reputation: 37
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).
Upvotes: 0
Views: 78
Reputation: 6292
You should implement IComparable if you want sort methods to use you comparison. I read between the lines, that that's what you want to do. Sorry, if I misunderstood. This is how you would do it:
abstract class Narys: IComparable
{
...
public int CompareTo(object obj)
{
if (obj == null) return 1;
Nardys otherMember = obj as Nardys;
if (otherMember != null)
{
// Check to see if last name is the same
if (this.Pavarde.CompareTo(otherMember.Pavarde)==0)
{
// Compare first names
return this.Vardas.CompareTo(otherMember.Vardas);
}
else
{
// Compare last names
return this.Pavarde.CompareTo(otherMemder.Pavarde);
}
}
else
{
throw new ArgumentException("Object is not a Nardys");
}
}
}
Now you can do an Array.Sort
or List.Sort
on the data. There is no need to overload operators for that purpose.
Upvotes: 0
Reputation: 45947
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); //syntax error here!
}
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;
}
Upvotes: 1