• 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

  • 相关阅读:
    Windows Azure Cloud Service (6) Reboot and Reimage
    Windows Azure Platform Introduction (7) Windows Azure Host OS 和 Guest OS
    Nod32的内网升级方案
    32位色
    ASP.NET 2.0 的内部变化
    关于Nod的离线升级方案
    光、色、补色
    解决迅雷的“重复任务提示”
    Nod32Viewer不能取得最新的镜像包的问题
    火车头实在是太好玩了
  • 原文地址:https://www.cnblogs.com/wt88/p/12090892.html
Copyright © 2020-2023  润新知