在上一篇中有朋友问实现根据多个列排序的问题,现在把修改过的代码放在这里。经过测试已经实现了目标效果。
实体类:
public class Car
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
private int weight;
![](/Images/OutliningIndicators/InBlock.gif)
public int Weight
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return weight; }
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ weight = value; }
}
private string type;
![](/Images/OutliningIndicators/InBlock.gif)
public string Type
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return type; }
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ type = value; }
}
}
适配器:
public class ComparaCarAdapter : IComparer<Car>
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
string[] _progNames;
List<PropertyInfo> pis = new List<PropertyInfo>();
List<string> sortTypes = new List<string>();
public ComparaCarAdapter(params string[] progNames)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
_progNames = progNames;
![](/Images/OutliningIndicators/InBlock.gif)
Type t = typeof(Car);
foreach (string progName in _progNames)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string[] progNameSplit = progName.Split('|');
string sortType = "";
PropertyInfo pi;
if (progNameSplit.Length > 1)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
sortType = progNameSplit[0];
pi = t.GetProperty(progNameSplit[1]);
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
pi = t.GetProperty(progName);
}
pis.Add(pi);
sortTypes.Add(sortType);
}
}
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
IComparer 成员IComparer 成员#region IComparer<Employee> 成员
![](/Images/OutliningIndicators/InBlock.gif)
public int Compare(Car x, Car y)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
int resualt = 0;
for (int i = 0; i < pis.Count; i++)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string sortType = sortTypes[i];
PropertyInfo pi = pis[i];
object xvalue = pi.GetValue(x, null);
object yvalue = pi.GetValue(y, null);
if (xvalue is string)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
resualt = ((string)xvalue).CompareTo((string)yvalue);
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (xvalue is int)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
resualt = ((int)xvalue).CompareTo((int)yvalue);
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new NotSupportedException();
}
}
if (sortType.ToUpper() == "DESC")
resualt = resualt * -1;
if (resualt != 0)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
break;
}
}
return resualt;
}
![](/Images/OutliningIndicators/InBlock.gif)
#endregion
}
测试代码:
public string test()
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
Car c1 = new Car();
c1.Weight = 200;
c1.Type = "T";
Car c2 = new Car();
c2.Weight = 500;
c2.Type = "b";
Car c3 = new Car();
c3.Weight = 300;
c3.Type = "b";
Car c4 = new Car();
c4.Weight = 300;
c4.Type = "t";
Car c5 = new Car();
c5.Weight = 200;
c5.Type = "d";
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
Car[] cars = new Car[]
{ c1, c2, c3, c4, c5 };
Array.Sort(cars,new ComparaCarAdapter("ASC|Weight","Desc|Type"));
string resualt = "";
foreach (Car c in cars)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
resualt += "|" + c.Weight + "," + c.Type;
}
return resualt;
}
说明:
new ComparaCarAdapter("ASC|Weight","Desc|Type");
参数需要中用“|”隔开,前面的代表排序方式(也可不写,如果不写也不用写分隔符),后面的为属性名称。
也可以将数组定义为ArrayList或List,并使用自带的Sort函数。
谢谢
雨中漫步的太阳的指正。
(转载请注明出处,并留言通知,谢谢。)