亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

請問在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條經驗 獲得超6個贊

用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 關注
  • 1722 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號