C# 实现二维数组的排序算法(代码)
class Order { /// <summary> /// 对二维数组排序 /// </summary> /// <param name="values">排序的二维数组</param> /// <param name="orderColumnsIndexs">排序根据的列的索引号数组</param> /// <param name="type">排序的类型,1代表降序,0代表升序</param> /// <returns>返回排序后的二维数组</returns> public static object[,] Orderby(object[,] values, int[] orderColumnsIndexs, int type) { object[] temp = new object[values.GetLength(1)]; int k; int compareResult; for (int i = 0; i < values.GetLength(0); i++) { for (k = i + 1; k < values.GetLength(0); k++) { if (type.Equals(1)) { for (int h = 0; h < orderColumnsIndexs.Length; h++) { compareResult = Comparer.Default.Compare(GetRowByID(values, k).GetValue(orderColumnsIndexs[h]), GetRowByID(values, i).GetValue(orderColumnsIndexs[h])); if (compareResult.Equals(1)) { temp = GetRowByID(values, i); Array.Copy(values, k * values.GetLength(1), values, i * values.GetLength(1), values.GetLength(1)); CopyToRow(values, k, temp); } if (compareResult != 0) break; } } else { for (int h = 0; h < orderColumnsIndexs.Length; h++) { compareResult = Comparer.Default.Compare(GetRowByID(values, k).GetValue(orderColumnsIndexs[h]), GetRowByID(values, i).GetValue(orderColumnsIndexs[h])); if (compareResult.Equals(-1)) { temp = GetRowByID(values, i); Array.Copy(values, k * values.GetLength(1), values, i * values.GetLength(1), values.GetLength(1)); CopyToRow(values, k, temp); } if (compareResult != 0) break; } } } } return values; } /// <summary> /// 获取二维数组中一行的数据 /// </summary> /// <param name="values">二维数据</param> /// <param name="rowID">行ID</param> /// <returns>返回一行的数据</returns> static object[] GetRowByID(object[,] values, int rowID) { if (rowID > (values.GetLength(0) - 1)) throw new Exception("rowID超出最大的行索引号!"); object[] row = new object[values.GetLength(1)]; for (int i = 0; i < values.GetLength(1); i++) { row[i] = values[rowID, i]; } return row; } /// <summary> /// 复制一行数据到二维数组指定的行上 /// </summary> /// <param name="values"></param> /// <param name="rowID"></param> /// <param name="row"></param> static void CopyToRow(object[,] values, int rowID, object[] row) { if (rowID > (values.GetLength(0) - 1)) throw new Exception("rowID超出最大的行索引号!"); if (row.Length > (values.GetLength(1))) throw new Exception("row行数据列数超过二维数组的列数!"); for (int i = 0; i < row.Length; 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 } }; Console.WriteLine("没排序前的二维数组:"); Print(o); Console.WriteLine("根据第3,4列升序排序后的数组:"); Order.Orderby(o, new int[] { 2,3 },0); Print(o); Console.WriteLine("根据第3,4列降序序排序后的数组:"); Order.Orderby(o, new int[] { 2, 3 }, 1); Print(o); Console.Read(); } static void Print(object[,] values) { int k; for (int i = 0; i < values.GetLength(0);i++ ) { for (k = 0; k < values.GetLength(1);k++ ) { Console.Write(values[i,k]); Console.Write(" "); } Console.WriteLine(" "); } }
没排序前的二维数组:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 11 12
15 16 11 17
5 6 7 9
根据第3,4列升序排序后的数组:
1 2 3 4
5 6 7 8
5 6 7 9
13 14 11 12
9 10 11 12
15 16 11 17
根据第3,4列降序序排序后的数组:
15 16 11 17
9 10 11 12
13 14 11 12
5 6 7 9
5 6 7 8
1 2 3 4