x

开发者

c# .net

导航

C#二维数组升/降序排列

代码
  1 /******************************************************************
  2 ** 文件名:         test_Default.cs
  3 ** 创建人:        党清华
  4 ** 创建日期:    2010-06-21
  5 ** 修改人:     
  6 ** 最后修改日期:
  7 ** 描 述:    将多维数组升/降序排列
  8 ** 版 本:1.0
  9 **---------------------------------------------------------------
 10 ** Copyright (c) 2010-2011 
 11 ******************************************************************/
 12 using System;
 13 using System.Web;
 14 public partial class test_Default : System.Web.UI.Page
 15 {
 16     private string temp0=null;
 17     private string temp1=null
 18     private string[,] x1 = new string[,] 
 19                                         { 
 20                                           {"0""0" },
 21                                           { "0""1" }, 
 22                                           { "1""0" }, 
 23                                           { "1""1" }, 
 24                                           { "26""0" }, 
 25                                           { "26""1" },   
 26                                           { "122""0" }, 
 27                                           { "122""1" }, 
 28                                           { "2""0" }, 
 29                                           { "2""1" }, 
 30                                           { "3""0" }, 
 31                                           { "3""1" } 
 32                                         };
 33     private string[,] x2 = new string[,] 
 34                                         { 
 35                                           {"1""0" },
 36                                           { "1""1" }, 
 37                                           { "2""0" }, 
 38                                           { "2""1" }, 
 39                                           { "5""0" }, 
 40                                           { "5""1" }, 
 41                                           { "6""0" }, 
 42                                           { "6""1" } 
 43                                         };
 44     private string[,] x3 = new string[,] 
 45                                         { 
 46                                           { "0""0" }, 
 47                                           { "0""1" }, 
 48                                           { "1""0" }, 
 49                                           { "1""1" }, 
 50                                           { "2""0" }, 
 51                                           { "2""1" }, 
 52                                           { "3""0" }, 
 53                                           { "3""1" }                                        
 54                                         };
 55     private string[,] x4 = new string[,] 
 56                                         { 
 57                                            { "3""0" },
 58                                            { "3""1" }, 
 59                                            { "2""0" }, 
 60                                            { "2""1" }, 
 61                                            { "1""0" }, 
 62                                            { "1""1" }, 
 63                                            { "0""0" }, 
 64                                            { "0""1" } 
 65                                         };
 66     protected void Page_Load(object sender, EventArgs e)
 67     {
 68         ListSorting(x1);
 69         Response.Write(WriteStr(x1));
 70     }
 71     /// <summary>
 72     /// 将二维数组升/降序排列(按上标最大值排列)
 73     /// </summary>
 74     /// <param name="x1">参数</param>
 75     ///<param name="Type">排序类型1为升序,0为降序</param>
 76     /// <returns></returns>
 77     private string[,] Change(string[,] val,int Type)
 78     {       
 79         for (int i = 0; i < (val.Length / 2); i++
 80         {
 81             for (int j = i + 1; j < (val.Length / 2); j++)
 82             {
 83                 if (Type ==1)
 84                 {
 85                     if (Convert.ToInt32(val[i, 0]) <= Convert.ToInt32(val[j, 0]))
 86                     {
 87                         temp0 = val[i, 0];                //0,0            
 88                         temp1 = val[i, 1];                //0,1                   
 89                         val[i, 0= val[j, 0];            //0,0=1,0
 90                         val[i, 1= val[j, 1];            //0,1=1,1                   
 91                         val[j, 0= temp0;                //1,0=0,0
 92                         val[j, 1= temp1;                //1,1=0,1
 93                         if (val[i, 0== val[j, 0])        //0,0=1,0
 94                         {
 95                             if (Convert.ToInt32(val[i, 1]) > Convert.ToInt32(val[j, 1]))  //0,1>1,1 
 96                             {
 97                                 temp0 = val[i, 1];       //0,1
 98                                 val[i, 1= val[j, 1];   //0,1=1,1
 99                                 val[j, 1= temp0;       //1,1                     
100                             }
101                         }
102                     }
103                 }
104                if(Type==0)
105                 {
106                     if (Convert.ToInt32(val[i, 0])>= Convert.ToInt32(val[j, 0]))
107                     {
108                         temp0 = val[i, 0];                //0,0            
109                         temp1 = val[i, 1];                //0,1                   
110                         val[i, 0= val[j, 0];            //0,0=1,0
111                         val[i, 1= val[j, 1];            //0,1=1,1                   
112                         val[j, 0= temp0;                //1,0=0,0
113                         val[j, 1= temp1;                //1,1=0,1
114                         if (val[i, 0== val[j, 0])        //0,0=1,0
115                         {
116                             if (Convert.ToInt32(val[i, 1]) > Convert.ToInt32(val[j, 1]))  //0,1>1,1 
117                             {
118                                 temp0 = val[i, 1];       //0,1
119                                 val[i, 1= val[j, 1];   //0,1=1,1
120                                 val[j, 1= temp0;       //1,1                     
121                             }
122                         }
123                     }
124                 }
125             }
126         }
127         return val;
128     }
129     /// <summary>
130     ///  将二维数降序排列(按降序将上标排列)
131     /// </summary>
132     /// <param name="val"></param>
133     private void ListSorting(string[,] val)
134     {
135         int strLength = val.Length / 2;        
136         for (int i = 0; i < strLength / 2; i++)
137         {
138             temp0 = val[i, 0];
139             temp1 = val[i, 1];
140             val[i, 0= val[strLength - 1 - i, 0];
141             val[i, 1= val[strLength - 1 - i, 1];
142             val[strLength - 1 - i, 0= temp0;
143             val[strLength - 1 - i, 1= temp1;
144         }
145         for (int i = 0; i < strLength; i++)
146         {
147             for (int j = i + 1; j < (val.Length / 2); j++)
148             {
149                 if (val[i, 0== val[j, 0])
150                 {
151                     if (Convert.ToInt32(val[i, 1]) > Convert.ToInt32(val[j, 1]))
152                     {
153                         temp0 = val[i, 1];
154                         val[i, 1= val[j, 1];
155                         val[j, 1= temp0;
156                     }
157                 }
158             }
159         }
160     }
161     /// <summary>
162     /// 输出二维数组
163     /// </summary>
164     /// <param name="val"></param>
165     /// <returns></returns>
166     private static string WriteStr(string[,] val)
167     {   
168         string str = "";
169         str += "{";
170         for (int i = 0; i < val.Length / 2; i++)
171         {
172             str += "{";
173             for (int j = 0; j < 2; j++)
174             {
175                 str += val[i, j] + ",";
176             }
177             str = str.Trim(','+ "},";            
178         }
179         str = str.Trim(','+  "}";
180         return str;
181     }
182 }
183 

 

posted on 2010-06-22 10:26  开发模式  阅读(1786)  评论(1编辑  收藏  举报

x