为了账号安全,请及时绑定邮箱和手机立即绑定

请问在c#中List<T>中sort函数的用法是?

请问在c#中List<T>中sort函数的用法是?

UYOU 2019-07-08 09:05:12
c#中List<T>中sort函数的用法是?
查看完整描述

6 回答

?
不负相思意

TA贡献1777条经验 获得超10个赞

这里有个例子, 你可以把参数 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);
}
}
}

参数就填这个方法名。



查看完整回答
反对 回复 2019-07-13
?
杨魅力

TA贡献1811条经验 获得超5个赞

用orderBy试试:
mlist.OrderBy(su => su.a).ThenBy(su=> su.b);

查看完整回答
反对 回复 2019-07-13
?
拉风的咖菲猫

TA贡献1995条经验 获得超2个赞

s  实际 是一个二维指针,直接传到sort里类型不符,sort只接受一维指针作为参数,传二维指针是无论如何也达不到目的的,要使一个函数能接收二维指针,必须指明大小,如
void example(int a[][5])
而sort当初这个接口设计的时候就没有考虑到二维指针,所以这里是行不通的。
PS:既然已经用了c++,C的一些东西能扔就扔,多用string,vector,list,还有些智能指针。少用些直接的指针,c++的指针出手一定是重量级的。


查看完整回答
反对 回复 2019-07-13
?
人到中年有点甜

TA贡献1895条经验 获得超7个赞

sort(s,s+n,cmp);
传入的参数是 s,可以理解为 str *,即 指向字符串的指针
使用 sort 需要处理几个问题:
1. ++操作;
2. 比较;
3. 赋值
你对于第三个没有处理,
假定有变量
str a,b;
a = b; // 这样是不可以的



查看完整回答
反对 回复 2019-07-13
  • 6 回答
  • 0 关注
  • 1633 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信