c# list 自定义排序
生活随笔
收集整理的這篇文章主要介紹了
c# list 自定义排序
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
// 要存在list中的節點信息class Node{public int nID;public int nValue;}// 打印list所有的節點static void print(List<Node> nodeList){for (int i = 0; i < nodeList.Count; i++){Console.WriteLine(nodeList[i].nID.ToString() + " , " + nodeList[i].nValue.ToString());}Console.WriteLine("");}static void Main(string[] args){// 創建一個list,里面隨機壓入一些節點信息List<Node> nodeList = new List<Node>();nodeList.Add(new Node() { nID = 1, nValue = 999 });nodeList.Add(new Node() { nID = 2, nValue = 67 });nodeList.Add(new Node() { nID = 5, nValue = 356 });nodeList.Add(new Node() { nID = 4, nValue = 20 });nodeList.Add(new Node() { nID = 3, nValue = 133 });// 以ID進行升序nodeList.Sort(delegate(Node n1, Node n2){return n1.nID.CompareTo(n2.nID);});Console.WriteLine("以ID進行升序后的結果:");print(nodeList);// 以ID進行降序nodeList.Sort(delegate(Node n1, Node n2){return n2.nID.CompareTo(n1.nID);});Console.WriteLine("以ID進行降序后的結果:");print(nodeList);// 以Value進行升序nodeList.Sort(delegate(Node n1, Node n2){return n1.nValue.CompareTo(n2.nValue);});Console.WriteLine("以Value進行升序后的結果:");print(nodeList);// 以Value進行降序nodeList.Sort(delegate(Node n1, Node n2){return n2.nValue.CompareTo(n1.nValue);});Console.WriteLine("以Value進行降序后的結果:");print(nodeList);Console.ReadKey();}
測試結果如下圖:
總結
以上是生活随笔為你收集整理的c# list 自定义排序的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: AOI之十字链表法
- 下一篇: go语言map按照key,value进行