学C#遇到了一个自己感觉挺难的问题,请各位大侠帮忙解决

总哈哈 2008-04-28 04:09:12
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);
}
}

我需要最后的结果是

1 0 a
3 1 c
2 1 b
4 2 d
5 4 e
6 5 f
...全文
218 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
iuhxq 2008-04-29
  • 打赏
  • 举报
回复
只要传递比较函数进去就可以

[Quote=引用 7 楼 cat_hsfz 的回复:]
你需要一个SortList()函数?其实不用的,你调用Sort()函数就是了,然后可以传递一个比较函数给它。

几乎所有现代编程语言都这样,内置排序算法,你给出比较函数就是了。例如你要排序的类型为getList,那么你的比较函数接受两个getList,如果第一个较大,返回一个大于0的值;如果第二个较大,返回一个小于0的值;如果两个相等,返回0。
[/Quote]
cat_hsfz 2008-04-29
  • 打赏
  • 举报
回复
你需要一个SortList()函数?其实不用的,你调用Sort()函数就是了,然后可以传递一个比较函数给它。

几乎所有现代编程语言都这样,内置排序算法,你给出比较函数就是了。例如你要排序的类型为getList,那么你的比较函数接受两个getList,如果第一个较大,返回一个大于0的值;如果第二个较大,返回一个小于0的值;如果两个相等,返回0。
Jacran 2008-04-29
  • 打赏
  • 举报
回复
向向net_lover提问听说是没有用的.
Magic_YJL 2008-04-29
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 shazibanzhu 的回复:]
我也来,帮顶
[/Quote]
changjiangzhibin 2008-04-29
  • 打赏
  • 举报
回复

//试试,未测试


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);
}
}
yunfeng007 2008-04-29
  • 打赏
  • 举报
回复

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
总哈哈 2008-04-29
  • 打赏
  • 举报
回复
花了半天时间搞定了,谢谢大家的支持!
sz_free_bird 2008-04-29
  • 打赏
  • 举报
回复
自己写个冒泡也行,如果你不嫌麻烦的话。
zhu_gx 2008-04-29
  • 打赏
  • 举报
回复
顶你
shazibanzhu 2008-04-28
  • 打赏
  • 举报
回复
我也来,帮顶
huaer1011 2008-04-28
  • 打赏
  • 举报
回复
sf

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧