This article describes the two-dimensional array sorting algorithm implemented by C#. Share it for your reference, as follows:
class Order { /// <summary> /// Sort two-dimensional arrays /// </summary> /// <param name="values">Sorted 2D array</param> /// <param name="orderColumnsIndexs">Array of index numbers of columns sorted by</param> /// <param name="type">Sorted type, 1 represents descending order, 0 represents ascending order</param> /// <returns>Returns the sorted two-dimensional array</returns> public static object[,] Orderby(object[,] values, int[] orderColumnsIndexs, int type) { object[] temp = new object[(1)]; int k; int compareResult; for (int i = 0; i < (0); i++) { for (k = i + 1; k < (0); k++) { if ((1)) { for (int h = 0; h < ; h++) { compareResult = (GetRowByID(values, k).GetValue(orderColumnsIndexs[h]), GetRowByID(values, i).GetValue(orderColumnsIndexs[h])); if ((1)) { temp = GetRowByID(values, i); (values, k * (1), values, i * (1), (1)); CopyToRow(values, k, temp); } if (compareResult != 0) break; } } else { for (int h = 0; h < ; h++) { compareResult = (GetRowByID(values, k).GetValue(orderColumnsIndexs[h]), GetRowByID(values, i).GetValue(orderColumnsIndexs[h])); if ((-1)) { temp = GetRowByID(values, i); (values, k * (1), values, i * (1), (1)); CopyToRow(values, k, temp); } if (compareResult != 0) break; } } } } return values; } /// <summary> /// Get the data of a row in a two-dimensional array /// </summary> /// <param name="values">2D data</param> /// <param name="rowID">Line ID</param> /// <returns>Returns a row of data</returns> static object[] GetRowByID(object[,] values, int rowID) { if (rowID > ((0) - 1)) throw new Exception("rowID exceeds the maximum row index number!"); object[] row = new object[(1)]; for (int i = 0; i < (1); i++) { row[i] = values[rowID, i]; } return row; } /// <summary> /// Copy a row of data to the specified row in the two-dimensional array /// </summary> /// <param name="values"></param> /// <param name="rowID"></param> /// <param name="row"></param> static void CopyToRow(object[,] values, int rowID, object[] row) { if (rowID > ((0) - 1)) throw new Exception("rowID exceeds the maximum row index number!"); if ( > ((1))) throw new Exception("The number of row data columns exceeds the number of columns in a two-dimensional array!"); for (int i = 0; i < ; i++) { values[rowID, i] = row[i]; } } } static void Main(string[] args) { object[,] o = new object[6, 4] { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 }, { 13, 14, 11, 12 }, { 15, 16, 11, 17, }, { 5, 6, 7,9 } }; ("The two-dimensional array before sorting:"); Print(o); ("Array sorted in ascending order according to columns 3 and 4:"); (o, new int[] { 2,3 },0); Print(o); ("Array sorted in descending order according to column 3 and 4:"); (o, new int[] { 2, 3 }, 1); Print(o); (); } static void Print(object[,] values) { int k; for (int i = 0; i < (0);i++ ) { for (k = 0; k < (1);k++ ) { (values[i,k]); (" "); } (" "); } }
Running results:
Two-dimensional array before sorting: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 11 12 15 16 11 17 5 6 7 9 According to3,4Arrays sorted in ascending order: 1 2 3 4 5 6 7 8 5 6 7 9 13 14 11 12 9 10 11 12 15 16 11 17 According to3,4Arrays sorted in descending order: 15 16 11 17 9 10 11 12 13 14 11 12 5 6 7 9 5 6 7 8 1 2 3 4
For more information about C# related content, please check out the topic of this site:Summary of C# array operation skills》、《Summary of C# traversal algorithm and skills》、《Summary of thread usage techniques for C# programming》、《Summary of XML file operation skills in C#》、《Tutorial on the usage of common C# controls》、《Summary of WinForm control usage》、《C# data structure and algorithm tutorial"and"Introduction to C# object-oriented programming tutorial》
I hope this article will be helpful to everyone's C# programming.