c#中List<T>中sort函数的用法是??
在最后有个问题要问大家的。。usingSystem;usingSystem.Collections.Generic;usingSystem.Text;namespace上...
在最后有个问题要问大家的。。
using System;
using System.Collections.Generic;
using System.Text;
namespace 上机实验5
{
class Program
{
struct stud
{
public string sname;
public int sno;
}
static void Main(string[] args)
{
int i;
List<stud> myset = new List<stud>();
stud s1 = new stud();
s1.sno = 101; s1.sname = "李明";
myset.Add(s1);
stud s2 = new stud();
s2.sno = 103; s2.sname = "王华";
myset.Add(s2);
stud s3 = new stud();
s3.sno = 108; s3.sname = "张英";
myset.Add(s3);
stud s4 = new stud();
s4.sno = 105; s4.sname = "张伟";
myset.Add(s4);
stud s5 = new stud();
s5.sno=109;s5.sname="哈哈";
Console.WriteLine("排序前:");
Console.Write("学号:");
foreach(stud st in myset)
{
Console.Write("{0,6}",st.sno);
}
Console.WriteLine();
Console.Write("姓名 : ");
foreach (stud st in myset)
{
Console.Write(st.sname+" ");
}
Console.WriteLine();
Console.WriteLine("按学号排序后:");
myset.Sort(这里希望是按学号排序,加什么参数呢??);
}
}
} 展开
using System;
using System.Collections.Generic;
using System.Text;
namespace 上机实验5
{
class Program
{
struct stud
{
public string sname;
public int sno;
}
static void Main(string[] args)
{
int i;
List<stud> myset = new List<stud>();
stud s1 = new stud();
s1.sno = 101; s1.sname = "李明";
myset.Add(s1);
stud s2 = new stud();
s2.sno = 103; s2.sname = "王华";
myset.Add(s2);
stud s3 = new stud();
s3.sno = 108; s3.sname = "张英";
myset.Add(s3);
stud s4 = new stud();
s4.sno = 105; s4.sname = "张伟";
myset.Add(s4);
stud s5 = new stud();
s5.sno=109;s5.sname="哈哈";
Console.WriteLine("排序前:");
Console.Write("学号:");
foreach(stud st in myset)
{
Console.Write("{0,6}",st.sno);
}
Console.WriteLine();
Console.Write("姓名 : ");
foreach (stud st in myset)
{
Console.Write(st.sname+" ");
}
Console.WriteLine();
Console.WriteLine("按学号排序后:");
myset.Sort(这里希望是按学号排序,加什么参数呢??);
}
}
} 展开
2个回答
展开全部
这里有个例子, 你可以把参数 sting换成的你的类型 stud然后相应用学号比较。
private static int CompareDinosByLength(string x, string y)
{
if (x == null)
{
if (y == null)
{
// If x is null and y is null, they're
// equal.
return 0;
}
else
{
// If x is null and y is not null, y
// is greater.
return -1;
}
}
else
{
// If x is not null...
//
if (y == null)
// ...and y is null, x is greater.
{
return 1;
}
else
{
// ...and y is not null, compare the
// lengths of the two strings.
//
int retval = x.Length.CompareTo(y.Length);
if (retval != 0)
{
// If the strings are not of equal length,
// the longer string is greater.
//
return retval;
}
else
{
// If the strings are of equal length,
// sort them with ordinary string comparison.
//
return x.CompareTo(y);
}
}
}
参数就填这个方法名。
private static int CompareDinosByLength(string x, string y)
{
if (x == null)
{
if (y == null)
{
// If x is null and y is null, they're
// equal.
return 0;
}
else
{
// If x is null and y is not null, y
// is greater.
return -1;
}
}
else
{
// If x is not null...
//
if (y == null)
// ...and y is null, x is greater.
{
return 1;
}
else
{
// ...and y is not null, compare the
// lengths of the two strings.
//
int retval = x.Length.CompareTo(y.Length);
if (retval != 0)
{
// If the strings are not of equal length,
// the longer string is greater.
//
return retval;
}
else
{
// If the strings are of equal length,
// sort them with ordinary string comparison.
//
return x.CompareTo(y);
}
}
}
参数就填这个方法名。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询