1 using System; 2 using System.Collections.Generic; 3 using System.Diagnostics; 4 using System.Linq; 5 using System.Text; 6 using System.Threading; 7 8 9 namespace ConsoleApplication1 10 { 11 class Program 12 { 13 static void Main(string[] args) 14 { 15 //定義一個二維list,用來代替二維數組,這樣每行的個數就可以變了 16 List<List<int>> array = new List<List<int>>(); 17 //定義一個一維list,作為上面二維list的某個元素 18 List<int> item = new List<int>(new int[] { 3, 4, 5, 6 }); 19 //將上面的一維list作為一個元素放入二維list中 20 array.Add(item); 21 //給一維list賦新值 22 item = new List<int>(new int[] { 30, 40, 50, 60 }); 23 //將上面的一維list作為一個元素放入二維list中 24 array.Add(item); 25 //給一維list賦新值 26 item = new List<int>(new int[] { 20, 40, 50, 30 }); 27 //將上面的一維list作為一個元素放入二維list中 28 array.Add(item); 29 30 31 //下面 取出二維list的某個元素 32 int m = array[1][2];//此時的m即為50 33 //下面 給二維list某位置賦值 34 array[1][2] = 60; 35 //驗證某位置的值是否改變 36 m = array[1][2]; 37 //將二維list的第0行所有元素給某個一維list 38 item = array[0]; 39 40 41 42 ////下面對二維list排序,且都是按最后一個元素排序,因為最后一個我作為遺傳算法的fit值 43 //list排序方法一 44 //array.Sort( delegate(List<int> p1,List<int> p2) 45 // { 46 // return p1[3].CompareTo(p2[3]);//按最后一個元素升序 47 // } 48 // );//升序或則用下面的排序http://blog.csdn.net/jimo_lonely/article/details/51711821 49 //list排序方法二 50 array.Sort((List<int> x, List<int> y) => { return x[3].CompareTo(y[3]); }); 51 //list排序方法三 52 List<List<int>> array1 = array.OrderBy(o => o[0]).ToList();//升序 53 54 //計時 55 Stopwatch sw = new Stopwatch(); 56 sw.Start(); 57 Thread.Sleep(2719); 58 sw.Stop(); 59 Console.WriteLine(sw.ElapsedTicks / (decimal)Stopwatch.Frequency); 60 Console.ReadKey(); 61 62 } 63 } 64 }

第一種排序最快
1 using System; 2 using System.Collections.Generic; 3 using System.Diagnostics; 4 using System.Linq; 5 using System.Text; 6 using System.Threading; 7 8 9 namespace ConsoleApplication1 10 { 11 class Program 12 { 13 static void Main(string[] args) 14 { 15 //定義一個二維list,用來代替二維數組,這樣每行的個數就可以變了 16 List<List<int>> array = new List<List<int>>(); 17 //定義一個一維list,作為上面二維list的某個元素 18 List<int> item = new List<int>(new int[] { 3, 4, 5, 6 }); 19 //將上面的一維list作為一個元素放入二維list中 20 array.Add(item); 21 //給一維list賦新值 22 item = new List<int>(new int[] { 30, 40, 50, 60 }); 23 //將上面的一維list作為一個元素放入二維list中 24 array.Add(item); 25 //給一維list賦新值 26 item = new List<int>(new int[] { 20, 40, 50, 30 }); 27 //將上面的一維list作為一個元素放入二維list中 28 array.Add(item); 29 List<List<int>> array1 = array; 30 List<List<int>> array2 = array; 31 List<List<int>> array3;//= array; 32 33 34 35 ////下面 取出二維list的某個元素 36 //int m = array[1][2];//此時的m即為50 37 ////下面 給二維list某位置賦值 38 //array[1][2] = 60; 39 ////驗證某位置的值是否改變 40 //m = array[1][2]; 41 ////將二維list的第0行所有元素給某個一維list 42 //item = array[0]; 43 44 45 //計時 46 Stopwatch sw = new Stopwatch(); 47 int s = 100000; 48 49 //下面對二維list排序,且都是按最后一個元素排序,因為最后一個我作為遺傳算法的fit值 50 //list排序方法一 51 sw.Start(); 52 for (int i = 0; i < s; i++) 53 { 54 array1.Sort(delegate(List<int> p1, List<int> p2) 55 { 56 return p1[3].CompareTo(p2[3]);//按最后一個元素升序 57 } 58 );//升序或則用下面的排序http://blog.csdn.net/jimo_lonely/article/details/51711821 59 sw.Stop(); 60 } 61 Console.WriteLine(sw.ElapsedTicks / (decimal)Stopwatch.Frequency); 62 63 64 //list排序方法二 65 sw.Start(); 66 for (int i = 0; i < s; i++) 67 { 68 array2.Sort((List<int> x, List<int> y) => { return x[3].CompareTo(y[3]); }); 69 sw.Stop(); 70 } 71 Console.WriteLine(sw.ElapsedTicks / (decimal)Stopwatch.Frequency); 72 73 //list排序方法三 74 sw.Start(); 75 for (int i = 0; i < s; i++) 76 { 77 array3 = array.OrderBy(o => o[0]).ToList();//升序 78 sw.Stop(); 79 } 80 Console.WriteLine(sw.ElapsedTicks / (decimal)Stopwatch.Frequency); 81 82 Console.ReadKey(); 83 84 85 86 87 88 ////計時 89 //Stopwatch sw = new Stopwatch(); 90 //sw.Start(); 91 ////Thread.Sleep(2719); 92 //sw.Stop(); 93 //Console.WriteLine(sw.ElapsedTicks / (decimal)Stopwatch.Frequency); 94 //Console.ReadKey(); 95 96 } 97 } 98 }
1 using System; 2 using System.Collections.Generic; 3 using System.Diagnostics; 4 using System.Linq; 5 using System.Text; 6 using System.Threading; 7 8 9 namespace ConsoleApplication1 10 { 11 class Program 12 { 13 static void Main(string[] args) 14 { 15 ////定義一個二維list,用來代替二維數組,這樣每行的個數就可以變了 16 //List<List<int>> array = new List<List<int>>(); 17 ////定義一個一維list,作為上面二維list的某個元素 18 //List<int> item = new List<int>(new int[] { 3, 4, 5, 6 }); 19 ////將上面的一維list作為一個元素放入二維list中 20 //array.Add(item); 21 ////給一維list賦新值 22 //item = new List<int>(new int[] { 30, 40, 50, 60 }); 23 ////將上面的一維list作為一個元素放入二維list中 24 //array.Add(item); 25 ////給一維list賦新值 26 //item = new List<int>(new int[] { 20, 40, 50, 30 }); 27 ////將上面的一維list作為一個元素放入二維list中 28 //array.Add(item); 29 //List<List<int>> array1 = array; 30 //List<List<int>> array2 = array; 31 //List<List<int>> array3;//= array; 32 33 34 35 ////下面 取出二維list的某個元素 36 //int m = array[1][2];//此時的m即為50 37 ////下面 給二維list某位置賦值 38 //array[1][2] = 60; 39 ////驗證某位置的值是否改變 40 //m = array[1][2]; 41 ////將二維list的第0行所有元素給某個一維list 42 //item = array[0]; 43 44 45 //計時 46 Stopwatch sw = new Stopwatch(); 47 int s = 100000; 48 49 //下面對二維list排序,且都是按最后一個元素排序,因為最后一個我作為遺傳算法的fit值 50 //list排序方法一 51 sw.Start(); 52 for (int i = 0; i < s; i++) 53 { 54 //定義一個二維list,用來代替二維數組,這樣每行的個數就可以變了 55 List<List<int>> array1 = new List<List<int>>(); 56 //定義一個一維list,作為上面二維list的某個元素 57 List<int> item = new List<int>(new int[] { 3, 4, 5, 6 }); 58 //將上面的一維list作為一個元素放入二維list中 59 array1.Add(item); 60 //給一維list賦新值 61 item = new List<int>(new int[] { 30, 40, 50, 60 }); 62 //將上面的一維list作為一個元素放入二維list中 63 array1.Add(item); 64 //給一維list賦新值 65 item = new List<int>(new int[] { 20, 40, 50, 30 }); 66 //將上面的一維list作為一個元素放入二維list中 67 array1.Add(item); 68 69 array1.Sort(delegate(List<int> p1, List<int> p2) 70 { 71 return p1[3].CompareTo(p2[3]);//按最后一個元素升序 72 } 73 );//升序或則用下面的排序http://blog.csdn.net/jimo_lonely/article/details/51711821 74 sw.Stop(); 75 } 76 Console.WriteLine(sw.ElapsedTicks / (decimal)Stopwatch.Frequency); 77 78 79 //list排序方法二 80 sw.Start(); 81 for (int i = 0; i < s; i++) 82 { 83 //定義一個二維list,用來代替二維數組,這樣每行的個數就可以變了 84 List<List<int>> array2 = new List<List<int>>(); 85 //定義一個一維list,作為上面二維list的某個元素 86 List<int> item = new List<int>(new int[] { 3, 4, 5, 6 }); 87 //將上面的一維list作為一個元素放入二維list中 88 array2.Add(item); 89 //給一維list賦新值 90 item = new List<int>(new int[] { 30, 40, 50, 60 }); 91 //將上面的一維list作為一個元素放入二維list中 92 array2.Add(item); 93 //給一維list賦新值 94 item = new List<int>(new int[] { 20, 40, 50, 30 }); 95 //將上面的一維list作為一個元素放入二維list中 96 array2.Add(item); 97 array2.Sort((List<int> x, List<int> y) => { return x[3].CompareTo(y[3]); }); 98 sw.Stop(); 99 } 100 Console.WriteLine(sw.ElapsedTicks / (decimal)Stopwatch.Frequency); 101 102 //list排序方法三 103 sw.Start(); 104 for (int i = 0; i < s; i++) 105 { 106 //定義一個二維list,用來代替二維數組,這樣每行的個數就可以變了 107 List<List<int>> array3 = new List<List<int>>(); 108 //定義一個一維list,作為上面二維list的某個元素 109 List<int> item = new List<int>(new int[] { 3, 4, 5, 6 }); 110 //將上面的一維list作為一個元素放入二維list中 111 array3.Add(item); 112 //給一維list賦新值 113 item = new List<int>(new int[] { 30, 40, 50, 60 }); 114 //將上面的一維list作為一個元素放入二維list中 115 array3.Add(item); 116 //給一維list賦新值 117 item = new List<int>(new int[] { 20, 40, 50, 30 }); 118 //將上面的一維list作為一個元素放入二維list中 119 array3.Add(item); 120 array3.OrderBy(o => o[0]).ToList();//升序 121 sw.Stop(); 122 } 123 Console.WriteLine(sw.ElapsedTicks / (decimal)Stopwatch.Frequency); 124 125 Console.ReadKey(); 126 127 128 129 130 131 ////計時 132 //Stopwatch sw = new Stopwatch(); 133 //sw.Start(); 134 ////Thread.Sleep(2719);//毫秒 135 //sw.Stop(); 136 //Console.WriteLine(sw.ElapsedTicks / (decimal)Stopwatch.Frequency); 137 //Console.ReadKey(); 138 139 } 140 } 141 }
