C# IComparable CompareTo(),如何在两个不同字段上实现比较
我有一门课是这样的:C# IComparable CompareTo(),如何在两个不同字段上实现比较,c#,.net,icomparable,C#,.net,Icomparable,我有一门课是这样的: public class Incident : IComparable<Incident> { public int Id { get; set; } public string Description { get; set; } public string IncidentType { get; set; } public int CompareTo(Incident other) { string s
public class Incident : IComparable<Incident>
{
public int Id { get; set; }
public string Description { get; set; }
public string IncidentType { get; set; }
public int CompareTo(Incident other)
{
string str = other.Description;
int ret = -1;
if (String.Compare(Description, str, StringComparison.OrdinalIgnoreCase) < 0)
ret = 1;
else if (String.Compare(Description, str, StringComparison.OrdinalIgnoreCase) > 0)
ret = -1;
else if (String.Compare(Description, str, StringComparison.OrdinalIgnoreCase) == 0)
ret = 0;
return ret;
}
}
公共类事件:i可比较
{
公共int Id{get;set;}
公共字符串说明{get;set;}
公共字符串IncidentType{get;set;}
公共int比较(事件其他)
{
字符串str=other.Description;
int-ret=-1;
if(String.Compare(Description,str,StringComparison.OrdinalIgnoreCase)<0)
ret=1;
else if(String.Compare(Description,str,StringComparison.OrdinalIgnoreCase)>0)
ret=-1;
else if(String.Compare(Description,str,StringComparison.OrdinalIgnoreCase)==0)
ret=0;
返回ret;
}
}
我可以根据描述字段按字母顺序对此类对象进行排序。
如何根据IncidentType字段对类进行排序
我不想同时对这两个字段进行排序。
有时我想按说明
对事件进行排序,有时在事件类型
公共整数比较(事件其他)
public int CompareTo(Incident other)
{
string str = other.IncidentType;
int ret = -1;
if (String.Compare(IncidentType, str, StringComparison.OrdinalIgnoreCase) < 0)
ret = 1;
else if (String.Compare(IncidentType, str, StringComparison.OrdinalIgnoreCase) > 0)
ret = -1;
else if (String.Compare(IncidentType, str, StringComparison.OrdinalIgnoreCase) == 0)
ret = 0;
return ret;
}
{
字符串str=other.IncidentType;
int-ret=-1;
if(String.Compare(IncidentType、str、StringComparison.OrdinalIgnoreCase)<0)
ret=1;
else if(String.Compare(IncidentType、str、StringComparison.OrdinalIgnoreCase)>0)
ret=-1;
else if(String.Compare(IncidentType、str、StringComparison.OrdinalIgnoreCase)==0)
ret=0;
返回ret;
}
记住,这只是IncidentType上的普通字符串比较。您可能需要在此处引入更复杂的业务规则。public int CompareTo(事件其他)
{
字符串str=other.IncidentType;
int-ret=-1;
if(String.Compare(IncidentType、str、StringComparison.OrdinalIgnoreCase)<0)
ret=1;
else if(String.Compare(IncidentType、str、StringComparison.OrdinalIgnoreCase)>0)
ret=-1;
else if(String.Compare(IncidentType、str、StringComparison.OrdinalIgnoreCase)==0)
ret=0;
返回ret;
}
记住,这只是IncidentType上的普通字符串比较。您可能需要在此处引入更复杂的业务规则。您可以在单独的类中实现
IComparer
,并且您可以在对元素排序时使用此比较器:
public class IncidentComparer : IComparer<Incident>
{
public int Compare(Incident x, Incident y)
{
return x.IncidentType.CompareTo(y.IncidentType);
}
}
您可以在单独的类中实现
IComparer
,并且在对元素排序时可以使用此比较器:
public class IncidentComparer : IComparer<Incident>
{
public int Compare(Incident x, Incident y)
{
return x.IncidentType.CompareTo(y.IncidentType);
}
}
可以在函数结束之前返回值,因此可以使用if语句检查ret是否为0,如果为true则返回ret。如果不是true,则将跳过If语句,并计算函数的其余部分。例如:
public class Incident : IComparable<Incident>
{
public int Id { get; set; }
public string Description { get; set; }
public string IncidentType { get; set; }
public int CompareTo(Incident other)
{
string str = other.Description;
int ret = String.Compare(str, Description, StringComparison.OrdinalIgnoreCase);
if (ret != 0)
return ret;
str = other.IncidentType;
ret = String.Compare(str, IncidentType, StringComparison.OrdinalIgnoreCase);
return ret;
}
}
公共类事件:i可比较
{
公共int Id{get;set;}
公共字符串说明{get;set;}
公共字符串IncidentType{get;set;}
公共int比较(事件其他)
{
字符串str=other.Description;
int-ret=String.Compare(str,Description,StringComparison.OrdinalIgnoreCase);
如果(ret!=0)
返回ret;
str=其他.IncidentType;
ret=String.Compare(str,IncidentType,StringComparison.OrdinalIgnoreCase);
返回ret;
}
}
将按描述排序,如果描述相同,则将按IncidentType排序。如果您想先按IncidentType排序,请切换这两个选项。您可以在函数结束之前返回一个值,这样您就可以使用If语句检查ret是否为0,如果为真,则返回ret。如果不是true,则将跳过If语句,并计算函数的其余部分。例如:
public class Incident : IComparable<Incident>
{
public int Id { get; set; }
public string Description { get; set; }
public string IncidentType { get; set; }
public int CompareTo(Incident other)
{
string str = other.Description;
int ret = String.Compare(str, Description, StringComparison.OrdinalIgnoreCase);
if (ret != 0)
return ret;
str = other.IncidentType;
ret = String.Compare(str, IncidentType, StringComparison.OrdinalIgnoreCase);
return ret;
}
}
公共类事件:i可比较
{
公共int Id{get;set;}
公共字符串说明{get;set;}
公共字符串IncidentType{get;set;}
公共int比较(事件其他)
{
字符串str=other.Description;
int-ret=String.Compare(str,Description,StringComparison.OrdinalIgnoreCase);
如果(ret!=0)
返回ret;
str=其他.IncidentType;
ret=String.Compare(str,IncidentType,StringComparison.OrdinalIgnoreCase);
返回ret;
}
}
将按描述排序,如果描述相同,则将按IncidentType排序。如果您想先按IncidentType排序,请切换这两种方式。如果您通常按
说明
或IncidentType进行排序,则可以实现不同的比较器,以便在不同的条件下进行排序
公共类事件:i可比较{
...
private IncidentTypeComparer类别:IComparer{
公共整数比较(事件x、事件y){
if(Object.ReferenceEquals(x,y))
返回0;
else if(Object.ReferenceEquals(x,null))
返回-1;
else if(Object.ReferenceEquals(null,y))
返回1;
返回String.Compare(x.IncidentType,y.IncidentType,StringComparison.OrdinalIgnoreCase);
}
}
//附加比较器(按意外事件类型)
IncidentTypeComparer的公共静态只读:IComparer=new IncidentTypeComparerClass();
...
}
...
列出事件=。。。
//按意外事件类型排序
事件.Sort(事件.ByIncidentTypeComparer);
...
//默认排序(根据描述)
事件。排序();
如果您只想按IncidentType进行一次或两次排序,则可以显式执行:
列出事件=。。。
事件.Sort((x,y)=>String.Compare(x.IncidentType,
y、 意外事故类型,
StringComparison(Ordinalingorecase));
如果这是一种典型的情况,您可以通过说明
或意外类型
进行排序,您可以实现不同的比较器,以在不同的条件下进行排序
公共类事件:i可比较{
public class Incident: IComparable<Incident> {
...
private IncidentTypeComparerClass: IComparer<Incident> {
public int Compare(Incident x, Incident y) {
if (Object.ReferenceEquals(x, y))
return 0;
else if (Object.ReferenceEquals(x, null))
return -1;
else if (Object.ReferenceEquals(null, y))
return 1;
return String.Compare(x.IncidentType, y.IncidentType, StringComparison.OrdinalIgnoreCase);
}
}
// Additional comparer, by IncidentType
public static readonly ByIncidentTypeComparer: IComparer<Incident> = new IncidentTypeComparerClass();
...
}
...
List<Incident> incidents = ...
// Sort by IncidentType
incidents.Sort(Incident.ByIncidentTypeComparer);
...
// Default sort (that's by Description)
incidents.Sort();
List<Incident> incidents = ...
incidents.Sort((x, y) => String.Compare(x.IncidentType,
y.IncidentType,
StringComparison.OrdinalIgnoreCase));
List<Indicent> listIncident = new List<Incident>();
//This to sort based on the incidentType
listIndicent = listIncident.OrderBy(x => x.IncidentType).ToList();
//This to sort based on the description
listIndicent = listIncident.OrderBy(x => x.Description).ToList();
//This to sort based on two
listIndicent = listIncident.OrderBy(x => x.Description).ThenBy(x => x.IncidentType).ToList();