public enum RespondantRole
{
Operator = 0,
Supervisor = 1,
Director = 2
}
public class Respondant: IComparable<Respondant>
{
public string Name { get; set; }
public RespondantRole Role { get; set; }
public int CompareTo(Respondant other)
{
return ((int)this.Role).CompareTo((int)other.Role);
}
}
我想创造一个
SortedSet
在这个班上。但我希望按角色排序,即操作员、主管和主管。但是,由于这不允许重复,我希望检查是否相等是基于
Name
因此,当我使用下面的代码初始化这个集合时,我得到了正确的顺序,但是只有3个元素,因为其他3个元素在后续的add中会出现重复的问题。
当唯一约束基于Name属性时,如何按角色对该集进行排序。
public class CallCenterManager
{
private SortedSet<Respondant> AvailableRespondants;
public CallCenterManager()
{
AvailableRespondants = new ObservableSortedSet<Respondant>();
for(int i=0; i < 10; ++i)
{
AvailableRespondants.Add(new Respondant() { Name = $"Operator{i}", Role = RespondantRole.Operator });
if(i < 5)
{
AvailableRespondants.Add(new Respondant() { Name = $"Supervisor{i}", Role = RespondantRole.Supervisor });
}
if(i < 3)
{
AvailableRespondants.Add(new Respondant() { Name = $"Director{i}", Role = RespondantRole.Director });
}
}
}
当我运行上面的代码时,我在集合中得到3个条目
{ Name = "Operator0", Role = Operator}
{ Name = "Supervisor0", Role = Supervisor}
{ Name = "Director0", Role = Director}
compareTo
是基于角色的下一套
Respondant
{ Name = "Operator1", Role = Operator}
所以我希望输出是
{ Name = "Operator0", Role = Operator}
{ Name = "Operator1", Role = Operator}
{ Name = "Operator2", Role = Operator}
.. upto 10 entries
{ Name = "Supervisor0", Role = Supervisor}
{ Name = "Supervisor1", Role = Supervisor}
.. up to 5 entries
{ Name = "Director0", Role = Director}
.. upto 3 entries