C#实现的二维数组排序算法示例

时间:2022-01-19 16:35:05

本文实例讲述了C#实现的二维数组排序算法。分享给大家供大家参考,具体如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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
15
16
17
18
19
20
21
没排序前的二维数组:
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

希望本文所述对大家C#程序设计有所帮助。

原文链接:http://www.cnblogs.com/xuanfeng/archive/2008/11/20/1334636.html