首页 > 代码库 > 二维数组按某列排序

二维数组按某列排序

技术分享
  1 using FrameWork.IoC.Achieve.IoCAbstractBasics;
  2 using FrameWork.IoC.Achieve.IoCBasics;
  3 using FrameWork.IoC.Case.Test;
  4 using FrameWork.IoC.Case.Test.TestOne;
  5 using FrameWork.IoC.Case.Test.TestTwo;
  6 using System;
  7 using System.Collections;
  8 using System.Collections.Generic;
  9 using System.Linq;
 10 using System.Text;
 11 using System.Threading.Tasks;
 12 
 13 namespace IoCDemo.Sort
 14 {
 15     class Program
 16     {
 17         static void Main(string[] args)
 18         {
 19            
 20             object [,] array = { { 60, 5 }, { 15, 5 }, { 30, 5 }, { 1, 5 }, { 2, 5 } };
 21             Console.WriteLine("没排序前的二维数组:");
 22             Print(array);
 23             Console.WriteLine("根据第1,2列升序排序后的数组:");
 24             Order.Orderby(array, new int[] { 0, 1 }, 0);
 25             Print(array);
 26 
 27             Console.WriteLine("根据第1,2列降序序排序后的数组:");
 28             Order.Orderby(array, new int[] { 1, 0 }, 1);
 29             Print(array);
 30             Console.ReadLine();
 31         }
 32         static void Print(object[,] values)
 33         {
 34             int k;
 35             for (int i = 0; i < values.GetLength(0); i++)
 36             {
 37                 for (k = 0; k < values.GetLength(1); k++)
 38                 {
 39                     Console.Write(values[i, k]);
 40                     Console.Write("  ");
 41                 }
 42                 Console.WriteLine(" ");
 43             }
 44         }
 45         class Order
 46         {
 47             /// <summary>
 48             /// 对二维数组排序
 49             /// </summary>
 50             /// <param name="values">排序的二维数组</param>
 51             /// <param name="orderColumnsIndexs">排序根据的列的索引号数组</param>
 52             /// <param name="type">排序的类型,1代表降序,0代表升序</param>
 53             /// <returns>返回排序后的二维数组</returns>
 54             public static object[,] Orderby(object[,] values, int[] orderColumnsIndexs, int type)
 55             {
 56                 object[] temp = new object[values.GetLength(1)];
 57                 int k;
 58                 int compareResult;
 59                 for (int i = 0; i < values.GetLength(0); i++)
 60                 {
 61                     for (k = i + 1; k < values.GetLength(0); k++)
 62                     {
 63                         if (type.Equals(1))
 64                         {
 65                             for (int h = 0; h < orderColumnsIndexs.Length; h++)
 66                             {
 67                                 compareResult = Comparer.Default.Compare(GetRowByID(values, k).GetValue(orderColumnsIndexs[h]), GetRowByID(values, i).GetValue(orderColumnsIndexs[h]));
 68                                 if (compareResult.Equals(1))
 69                                 {
 70                                     temp = GetRowByID(values, i);
 71                                     Array.Copy(values, k * values.GetLength(1), values, i * values.GetLength(1), values.GetLength(1));
 72                                     CopyToRow(values, k, temp);
 73                                 }
 74                                 if (compareResult != 0)
 75                                     break;
 76                             }
 77                         }
 78                         else
 79                         {
 80                             for (int h = 0; h < orderColumnsIndexs.Length; h++)
 81                             {
 82                                 compareResult = Comparer.Default.Compare(GetRowByID(values, k).GetValue(orderColumnsIndexs[h]), GetRowByID(values, i).GetValue(orderColumnsIndexs[h]));
 83                                 if (compareResult.Equals(-1))
 84                                 {
 85                                     temp = GetRowByID(values, i);
 86                                     Array.Copy(values, k * values.GetLength(1), values, i * values.GetLength(1), values.GetLength(1));
 87                                     CopyToRow(values, k, temp);
 88                                 }
 89                                 if (compareResult != 0)
 90                                     break;
 91                             }
 92                         }
 93                     }
 94                 }
 95                 return values;
 96 
 97             }
 98             /// <summary>
 99             /// 获取二维数组中一行的数据
100             /// </summary>
101             /// <param name="values">二维数据</param>
102             /// <param name="rowID">行ID</param>
103             /// <returns>返回一行的数据</returns>
104             static object[] GetRowByID(object[,] values, int rowID)
105             {
106                 if (rowID > (values.GetLength(0) - 1))
107                     throw new Exception("rowID超出最大的行索引号!");
108 
109                 object[] row = new object[values.GetLength(1)];
110                 for (int i = 0; i < values.GetLength(1); i++)
111                 {
112                     row[i] = values[rowID, i];
113 
114                 }
115                 return row;
116 
117             }
118             /// <summary>
119             /// 复制一行数据到二维数组指定的行上
120             /// </summary>
121             /// <param name="values"></param>
122             /// <param name="rowID"></param>
123             /// <param name="row"></param>
124             static void CopyToRow(object[,] values, int rowID, object[] row)
125             {
126                 if (rowID > (values.GetLength(0) - 1))
127                     throw new Exception("rowID超出最大的行索引号!");
128                 if (row.Length > (values.GetLength(1)))
129                     throw new Exception("row行数据列数超过二维数组的列数!");
130                 for (int i = 0; i < row.Length; i++)
131                 {
132                     values[rowID, i] = row[i];
133                 }
134             }
135         }
136     }
137 }
View Code

技术分享

 

二维数组按某列排序