62,046
社区成员
发帖
与我相关
我的任务
分享
//试试,未测试
static void Main(string[] args)
{
SortedList sl = new SortedList();
sl.Add(new getList(2, 1, "b"));
sl.Add(new getList(3, 1, "c"));
sl.Add(new getList(4, 2, "d"));
sl.Add(new getList(1, 0, "a"));
sl.Add(new getList(5, 4, "e"));
sl.Add(new getList(6, 5, "f"));
string BlankBase = new string(' ', 8);
Console.WriteLine("排序以前的对象");
foreach(getList g in sl)
{
Console.WriteLine(g.id + BlankBase + g.parentId + BlankBase + g.name);
}
sl.Sort();//在IDE中查看一下
Console.WriteLine("\r\n\r\n排序以后的对象");
foreach(getList g in sl)
{
Console.WriteLine(g.id + BlankBase + g.parentId + BlankBase + g.name);
}
}
using System;
namespace my
{
class getList
{
public getList(int id,int parentid,string name)
{
this._id = id;
this._parentId = parentid;
this._name = name;
}
private int _id;
public int id
{
get{return _id;}
set{this._id = value;}
}
private int _parentId;
public int parentId
{
get{return _parentId;}
set{this._parentId = value;}
}
private string _name;
public string name
{
get{return _name;}
set{this._name = value;}
}
}
class myClass
{
static getList[] SortList(getList[] outList)
{
getList tempgetList = new getList(0,0,"");
for(int i=0;i < outList.Length; i++)
{
for(int j = outList.Length -2 ; j>=i;j--)
{
if(outList[j+1].parentId<outList[j].parentId)
{
tempgetList = outList[j+1];
outList[j+1] = outList[j];
outList[j] = tempgetList;
}
}
}
return outList;
}
static void Main(string[] args)
{
// 当前id 父id 内容
getList[] ourList = new getList[] {
new getList(2, 1, "b"),
new getList(3, 1, "c"),
new getList(4, 2, "d"),
new getList(1, 0, "a"),
new getList(5, 4, "e"),
new getList(6, 5, "f")
};
string BlankBase = new string(' ', 8);
Console.WriteLine("排序以前的对象");
for (int i = 0; i < ourList.Length; i++)
{
Console.WriteLine(ourList[i].id + BlankBase + ourList[i].parentId + BlankBase + ourList[i].name);
}
ourList = SortList(ourList);
Console.WriteLine("\r\n\r\n排序以后的对象");
for (int i = 0; i < ourList.Length; i++)
{
Console.WriteLine(ourList[i].id + BlankBase + ourList[i].parentId + BlankBase + ourList[i].name);
}
}
}
}
结果
排序以前的对象
2 1 b
3 1 c
4 2 d
1 0 a
5 4 e
6 5 f
排序以后的对象
1 0 a
2 1 b
3 1 c
4 2 d
5 4 e
6 5 f