C 中List的排序

2021-07-05 17:34:36 字數 887 閱讀 4968

前些天寫程式時遇到這樣乙個問題:在乙個list中儲存了幾個point型別的座標,需要根據這些座標的x軸數值進行排序當時的想法是使用list.orderby方法

list1.orderby(p => p.x);  //list1中儲存了若干point變數
但是list1並沒有像預期的那樣,按照x軸座標排序,後來查了一些資料後,找到了解決辦法:宣告乙個ienumerable

<

t>  列舉器,然後把orderby的結果傳遞給它

ienumerabletemp;

temp = list1.orderby(p => p.x);

point min = temp.elementat(0); //使用elementat() 獲取第乙個資料

回到正題,說一下list的排序

一、繼承icomparable介面,還要實現compareto()方法,呼叫時,直接使用sort()方法即可

static void main(string args));

stulist.add(new student() );

stulist.add(new student() );

stulist.sort();

console.writeline(stulist[0].name + ":" + stulist[0].age);

console.readkey();

}public class student : icomparable

public string name

public int compareto(object obj)

}

C 中List的排序

c 中list可以儲存任意型別的物件,本文實現一種較為簡單的排序方法 1 定義類物件 public class cattributefeature public double m dattributefeature public cattributefeature string strname,do...

C 中List的排序用法 Sort

要對自定義類陣列或list進行排序,譬如 listuserlist arraylist arraylist 最重要的是 繼承icomparer介面,實現int icomparer.compare t t1,t t2 方法。如下 繼承icomparer介面,實現同一自定義型別 物件比較 t為泛用型別 ...

C 中vector和list排序

容器 泛型演算法 和類是不是就是c 相對於c 的那部分呢?暫時先這麼認為吧。如果這篇部落格有幸被別人看到,請幫忙指出。c 菜鳥 留。vector的迭代器是隨機訪問迭代器,支援泛型演算法的sort及其演算法。vector排序 include include include include includ...