class Pxu : IComparable
{
public string FirstName;
public string LastName;
//public string FirstName
//{
// get;
// set;
//}
//public string LastName
//{
// get;
// set;
//}
public Pxu(string str1, string str2)
{
this.FirstName = str1;
this.LastName = str2;
}
public int CompareTo(object o)
{
Pxu other = o as Pxu;
int result= this.FirstName.CompareTo(other.FirstName);
if (result == 0)
result = this.LastName.CompareTo(other.LastName);
return result;
}
} Pxu[] p ={
new Pxu("ww","eee"),
new Pxu("ytet","sdfer"),
new Pxu("uyte","dfeh"),
};
Array.Sort(p);
foreach (Pxu px in p)
{
Console.WriteLine(px);
}输出得不到我想要的结果 我想要的结果当然是排序而这个输出的是ConsoleApplication2.PxuConsoleApplication2.PxuConsoleApplication2.Pxu输出三次。就解答为什么会是这样
{
public string FirstName;
public string LastName;
//public string FirstName
//{
// get;
// set;
//}
//public string LastName
//{
// get;
// set;
//}
public Pxu(string str1, string str2)
{
this.FirstName = str1;
this.LastName = str2;
}
public int CompareTo(object o)
{
Pxu other = o as Pxu;
int result= this.FirstName.CompareTo(other.FirstName);
if (result == 0)
result = this.LastName.CompareTo(other.LastName);
return result;
}
} Pxu[] p ={
new Pxu("ww","eee"),
new Pxu("ytet","sdfer"),
new Pxu("uyte","dfeh"),
};
Array.Sort(p);
foreach (Pxu px in p)
{
Console.WriteLine(px);
}输出得不到我想要的结果 我想要的结果当然是排序而这个输出的是ConsoleApplication2.PxuConsoleApplication2.PxuConsoleApplication2.Pxu输出三次。就解答为什么会是这样
{
Console.WriteLine(px.FirstName + px.LastName);
}要么你重写Pxu类的ToString()
public Pxu(string str1, string str2)
{
this.FirstName = str1;
this.LastName = str2;
}
public int CompareTo(object o)
{
Pxu other = o as Pxu;
int result= this.FirstName.CompareTo(other.FirstName);
if (result == 0)
result = this.LastName.CompareTo(other.LastName);
return result;
}
public override string ToString()
{
return string.Format("FirstName:{0},LastName:{1}", this.FirstName, this.LastName);
}
}
Pxu[] p ={
new Pxu("ww","eee"),
new Pxu("ytet","sdfer"),
new Pxu("uyte","dfeh"),
};
Array.Sort(p);
foreach (Pxu px in p)
{
Console.WriteLine(px.ToString());
}