asp.net里面对foreach输出泛型有办法排序吗? 50
publicstaticIEnumerable<t_Students>Students;protectedvoidPage_Load(objectsender,Event...
public static IEnumerable<t_Students> Students;
protected void Page_Load(object sender, EventArgs e)
{
foreach (t_Students sd in Students)
{
Response.Write(sd.id);
Response.Write(sd.name);
Response.Write("<br/>");
}
}
我想让他输出时根据id的顺序来,请问有办法吗?
或者用其它方法可以吗? 展开
protected void Page_Load(object sender, EventArgs e)
{
foreach (t_Students sd in Students)
{
Response.Write(sd.id);
Response.Write(sd.name);
Response.Write("<br/>");
}
}
我想让他输出时根据id的顺序来,请问有办法吗?
或者用其它方法可以吗? 展开
3个回答
展开全部
你可以用List来保存t_Students然后自定义一个排序方法进行排序,如下
using System.Collections.Generic;
public class t_Students
{
public t_Students(int id, string name)
{
this.id = id;
this.name = name;
}
public int id;
public string name;
}
public int StudentsComparer(t_Students x, t_Students y)
{
if (x.id < y.id)
return -1;
else
return 1;
}
public static List<t_Students> Students;
protected void Page_Load(object sender, EventArgs e)
{
Students = new List<t_Students>();
Students.Add(new t_Students(5, "5555"));
Students.Add(new t_Students(1, "1111"));
Students.Add(new t_Students(3, "3333"));
Students.Add(new t_Students(7, "7777"));
Students.Add(new t_Students(1, "1111"));
Students.Add(new t_Students(4, "4444"));
Students.Sort(StudentsComparer);
foreach (t_Students sd in Students)
{
Response.Write(sd.id);
Response.Write(sd.name);
Response.Write("<br/>");
}
}
结果
11111
11111
33333
44444
55555
77777
using System.Collections.Generic;
public class t_Students
{
public t_Students(int id, string name)
{
this.id = id;
this.name = name;
}
public int id;
public string name;
}
public int StudentsComparer(t_Students x, t_Students y)
{
if (x.id < y.id)
return -1;
else
return 1;
}
public static List<t_Students> Students;
protected void Page_Load(object sender, EventArgs e)
{
Students = new List<t_Students>();
Students.Add(new t_Students(5, "5555"));
Students.Add(new t_Students(1, "1111"));
Students.Add(new t_Students(3, "3333"));
Students.Add(new t_Students(7, "7777"));
Students.Add(new t_Students(1, "1111"));
Students.Add(new t_Students(4, "4444"));
Students.Sort(StudentsComparer);
foreach (t_Students sd in Students)
{
Response.Write(sd.id);
Response.Write(sd.name);
Response.Write("<br/>");
}
}
结果
11111
11111
33333
44444
55555
77777
展开全部
你是想按照学号排序?
按照学号排序,你可以用
SortedList这个数据结构,具体用法是
SortedList<t_Student> sObj = new SortedList<t_Student>();
使用方法 同List链表一样。
List是不排序链表,SortList是排序链表
参考以下网站:
http://hi.baidu.com/janksandks/blog/item/49c4b83ee077303471cf6c38.html
按照学号排序,你可以用
SortedList这个数据结构,具体用法是
SortedList<t_Student> sObj = new SortedList<t_Student>();
使用方法 同List链表一样。
List是不排序链表,SortList是排序链表
参考以下网站:
http://hi.baidu.com/janksandks/blog/item/49c4b83ee077303471cf6c38.html
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
IList<Students> list=new List<Students>();
list.Add(new Students(1,"张三","123");
list.Add(new Students(5,"李四","123");
list.Add(new Students(3,"王五","123");
//这是正序
list=list.OrderBy(s=>s.id).ToList();
//这是倒序
list=list.OrderByDescending(s=>s.id).ToList();
list.Add(new Students(1,"张三","123");
list.Add(new Students(5,"李四","123");
list.Add(new Students(3,"王五","123");
//这是正序
list=list.OrderBy(s=>s.id).ToList();
//这是倒序
list=list.OrderByDescending(s=>s.id).ToList();
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询