list<>.Sort() 怎么用

lude8880 2010-07-01 05:55:56
参数不会写,谁给个例子
...全文
2554 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
songy_2008 2010-07-02
  • 打赏
  • 举报
回复
List<Racer> racers = new List<Racer>();
racers.add.........

racers.Sort(delegate(Racer r1, Racer r2)
{
return r1.Name.CompareTo(r2.Name);
});
骑猪看海 2010-07-02
  • 打赏
  • 举报
回复
如果你用的是3.5的framework,很简单。
list.Sort((customer1, customer2) => customer1.CompareTo(customer2));

如果你不是3.5,用上面的语句编译出错的话,可以用显示的委托:

list.Sort(delegate(string customer1, string customer2) { return customer1.CompareTo(customer2); });

别递烟哥不会 2010-07-02
  • 打赏
  • 举报
回复
Ilist<int> list=new List<int>();
{赋值}
list.sort()`
老Key 2010-07-02
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 wuyq11 的回复:]
List<T> t=new List<T>();
t.Sort(delegate( t p1, t p2) { return p1.ID.CompareTo(p2.ID); });

public class Student
{
public string Name{get;set;}
public string Age{get;set;}
}
public class Stu……
[/Quote]b不错。。。
nocallstle 2010-07-02
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 symbol_bc 的回复:]

C# code

public class DesignFieldSort : IComparer<LSFormDesigner.IDesignFieldSearch>
{

#region IComparer<DesignForm> 成员

public int Compare(IDesignFieldSearch x, IDesignFieldSea……
[/Quote]
就是这个
lude8880 2010-07-02
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 wuyq11 的回复:]

List<T> t=new List<T>();
t.Sort(delegate( t p1, t p2) { return p1.ID.CompareTo(p2.ID); });

public class Student
{
public string Name{get;set;}
public string Age{get;set;}
}
public class St……
[/Quote]

使用起来是不是:


Student student;...

student.Sort(new StudentComparer());
?

柳晛 2010-07-01
  • 打赏
  • 举报
回复
            int len = 100;

List<int> lst = new List<int>();

Random ra = new Random();

for (int i = 0; i < len; i++)
{
lst.Add(ra.Next(0, 10));
}

lst.Sort();

for (int i = 0; i < len; i++)
{
Console.WriteLine(lst[i]);
}
龙宜坡 2010-07-01
  • 打赏
  • 举报
回复
这问题貌似MSDN上就有的
telankes2000 2010-07-01
  • 打赏
  • 举报
回复
頂如夢
就是這樣子滴
wuyq11 2010-07-01
  • 打赏
  • 举报
回复
List<T> t=new List<T>();
t.Sort(delegate( t p1, t p2) { return p1.ID.CompareTo(p2.ID); });

public class Student
{
public string Name{get;set;}
public string Age{get;set;}
}
public class StudentComparer : IComparer<Student>
{
public enum CompareType
{
Name,
Age
}
private CompareType type;
public StudentComparer(CompareType type)
{
this.type = type;
}
public int Compare(Student x, Student y)
{
switch(this.type)
{
case CompareType.Name:
return x.Name.CompareTo(y.Name);
defaqult:
return x.Age.CompareTo(y.Age);
}
}
}
symbol_bc 2010-07-01
  • 打赏
  • 举报
回复

public class DesignFieldSort : IComparer<LSFormDesigner.IDesignFieldSearch>
{

#region IComparer<DesignForm> 成员

public int Compare(IDesignFieldSearch x, IDesignFieldSearch y)
{
return x.SortValue.CompareTo(y.SortValue);
}

#endregion

}

void main()
{
List<LSFormDesigner.IDesignFieldSearch> list = new List<>(LSFormDesigner.IDesignFieldSearch);
list.Sort(new DesignFieldSort ());
}
IHandler 2010-07-01
  • 打赏
  • 举报
回复
边城的刀声 2010-07-01
  • 打赏
  • 举报
回复
classes.Sort(delegate(Type type1, Type type2) { return type1.Name.CompareTo(type2.Name); });
a511900703 2010-07-01
  • 打赏
  • 举报
回复
list<>.Sort();
list<>这个里面是要传对象!

62,074

社区成员

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

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

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

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